Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Buku Telepon
- * Aplikasi untuk menyimpan buku telepon atau kontak
- *
- * @author Agus Susilo
- * @date 13 November 2015 22:00
- * @version 0.1
- * @copyright (C) 2015 Susilolab
- */
- namespace PhoneBook
- {
- public class PhoneBookForm: Gtk.Application
- {
- public delegate void ToolButtonEvent();
- private Gtk.Builder builder;
- private Gtk.Window main_window;
- private Gtk.ImageMenuItem mnPhoneBook;
- private Gtk.ImageMenuItem mnQuit;
- private Gtk.ImageMenuItem mnAddGroup;
- private Gtk.ImageMenuItem mnAbout;
- private Gtk.ImageMenuItem mnRefresh;
- private Gtk.ScrolledWindow swPhoneBook;
- private Gtk.ToolItem tItemAdd;
- private Gtk.ToolItem tItemEdit;
- private Gtk.ToolItem tItemDelete;
- private Gtk.Toolbar toolbar;
- private Gtk.Box box;
- private Gtk.Box boxGrid;
- private Gtk.TreeView view;
- private Gtk.ListStore list_store = new Gtk.ListStore(6, typeof(string), typeof(string), typeof(string),
- typeof(string), typeof(string), typeof(string));
- private Sqlite.Database db;
- private const string dbName = "share/data/phonebook.db";
- public PhoneBookForm() {
- Object(application_id: "com.susilolabs.phonebook", flags: ApplicationFlags.FLAGS_NONE);
- }
- protected override void activate() {
- try {
- builder = new Gtk.Builder();
- builder.add_from_file("share/ui/phonebook.glade");
- builder.connect_signals(null);
- this.main_window = builder.get_object("winPhoneBook") as Gtk.Window;
- this.main_window.application = this;
- //this.main_window.set_position(WindowPosition.CENTER);
- swPhoneBook = builder.get_object("swPhoneBook") as Gtk.ScrolledWindow;
- box = builder.get_object("box1") as Gtk.Box;
- mnPhoneBook = builder.get_object("mnPhoneBook") as Gtk.ImageMenuItem;
- mnPhoneBook.activate.connect(mnPhoneBook_activate);
- mnRefresh = builder.get_object("mnRefresh") as Gtk.ImageMenuItem;
- mnRefresh.activate.connect(refresh_table_contact);
- mnQuit = builder.get_object("mnQuit") as Gtk.ImageMenuItem;
- mnQuit.activate.connect( () => {
- this.quit();
- });
- mnAddGroup = builder.get_object("mnAddGroup") as Gtk.ImageMenuItem;
- mnAddGroup.activate.connect( () => {
- AddGroup group = new AddGroup.with_parent_window(this.main_window);
- });
- mnAbout = builder.get_object("mnAbout") as Gtk.ImageMenuItem;
- add_toolbar();
- components_init();
- db_init();
- builder.connect_signals(this);
- this.main_window.show_all();
- }catch(Error e) {
- Utils.MessageBox(this.main_window, e.message, "Error saat memuat layout file");
- stderr.printf("%s\n", e.message);
- }
- }
- private Gtk.ToolItem new_item_button(string label, Gtk.Image img, ToolButtonEvent? evt=null) {
- var item = new Gtk.ToolItem();
- var btn = new Gtk.Button.with_label(label);
- btn.set_image(img);
- if(evt != null) {
- ToolButtonEvent ev = evt;
- btn.clicked.connect(ev());
- }
- btn.set_margin_left(5);
- btn.set_margin_top(5);
- btn.set_margin_right(5);
- btn.set_margin_bottom(5);
- item.add(btn);
- return item;
- }
- private void add_toolbar() {
- toolbar = builder.get_object("toolbar1") as Gtk.Toolbar;
- var imgAdd = new Gtk.Image.from_file("share/icons/add.png");
- ToolButtonEvent evtAdd = () => { stdout.printf("Tombol tambah diklik.\n"); };
- tItemAdd = new_item_button("Tambah", imgAdd, evtAdd);
- toolbar.add(tItemAdd);
- var imgEdit = new Gtk.Image.from_file("share/icons/pencil.png");
- tItemEdit = new_item_button("Ubah", imgEdit);
- toolbar.add(tItemEdit);
- var imgDelete = new Gtk.Image.from_file("share/icons/delete.png");
- tItemDelete = new_item_button("Hapus", imgDelete);
- toolbar.add(tItemDelete);
- }
- private void mnPhoneBook_activate() {
- AddContactForm app = new AddContactForm.with_parent_window(main_window);
- app.set_parent_app(this);
- }
- private List<Contact> get_contact() {
- List<Contact> contacts = new List<Contact>();
- File dbFile = File.new_for_path(dbName);
- int ec = 0;
- if(dbFile.query_exists(null)) {
- ec = Sqlite.Database.open(dbFile.get_path(), out db);
- }
- if(ec != Sqlite.OK) {
- stderr.printf("Tidak dapat membuka database: %s\n", db.errmsg());
- }
- string sql = "SELECT pb.id AS id, c.name AS cat_name, pb.name, pb.company, pb.title, ";
- sql += "pb.phone_number, pb.email FROM phone_book pb LEFT JOIN category c ";
- sql += "ON pb.category_id=c.id;";
- string errMsg;
- string[] res;
- int nrows;
- int ncols;
- ec = db.get_table(sql, out res, out nrows, out ncols, out errMsg);
- if(ec != Sqlite.OK) {
- stderr.printf("Error saat select: %s\n", errMsg);
- }
- int max = nrows * ncols + ncols;
- for(int i=0; i<max; i=i+ncols) {
- Contact contact = new Contact();
- if(i == 0)
- continue;
- else {
- for(int j=0; j<ncols; j++) {
- if(j == 0) {
- contact.id = int.parse(res[i+j]);
- }else if(j == 1) {
- contact.category_name = res[i+j];
- }else if(j == 2) {
- contact.name = res[i+j];
- }else if(j == 3) {
- contact.company = res[i+j];
- }else if(j == 4) {
- contact.title = res[i+j];
- }else if(j == 5) {
- contact.phone_number = res[i+j];
- }else if(j == 6) {
- contact.email = res[i+j];
- }
- }
- }
- if(i > 0) contacts.append(contact);
- }
- return contacts;
- }
- public void refresh_table_contact() {
- list_store.clear();
- Gtk.TreeIter iter;
- List<Contact> contact = get_contact();
- foreach(Contact c in contact) {
- list_store.append(out iter);
- list_store.set(iter, 0, c.title, 1, c.name, 2, c.phone_number, 3, c.category_name,
- 4, c.email, 5, c.company);
- }
- view.set_model(list_store);
- }
- private void show_table_contact() {
- string[] cols = { "Judul", "Nama", "No. HP", "Grup", "Email", "Perusahaan" };
- boxGrid = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
- Gtk.TreeIter iter;
- List<Contact> contact = get_contact();
- foreach(Contact c in contact) {
- list_store.append(out iter);
- list_store.set(iter, 0, c.title, 1, c.name, 2, c.phone_number, 3, c.category_name,
- 4, c.email, 5, c.company);
- }
- view = new Gtk.TreeView.with_model(list_store);
- view.headers_clickable = true;
- view.show_expanders = true;
- boxGrid.pack_start(view, true, true, 0);
- swPhoneBook.add(boxGrid);
- Gtk.CellRendererText cell = new Gtk.CellRendererText();
- cell.editable = false;
- int i = 0;
- foreach(string col in cols) {
- view.insert_column_with_attributes(-1, col, cell, "text", i);
- i++;
- }
- }
- private void components_init() {
- show_table_contact();
- }
- private void db_init() {
- File dbFile = File.new_for_path("share/data/phonebook.db");
- int ec = 0;
- if(!dbFile.query_exists(null)) {
- ec = Sqlite.Database.open(dbFile.get_path(), out db);
- }
- if(ec != Sqlite.OK) {
- Utils.MessageBox(null, "Tidak dapat membuka database: " + db.errmsg(), "Error sqlite");
- }
- string sql = """
- CREATE TABLE IF NOT EXISTS phone_book(
- id INT PRIMARY KEY NOT NULL,
- category_id INT NOT NULL,
- name TEXT NOT NULL,
- company TEXT NOT NULL,
- title TEXT NOT NULL,
- phone_number TEXT NOT NULL,
- email TEXT NOT NULL
- );
- CREATE TABLE IF NOT EXISTS category(
- id INT PRIMARY KEY NOT NULL,
- name TEXT NOT NULL
- );
- """;
- string err;
- ec = db.exec(sql, null, out err);
- if(ec != Sqlite.OK) {
- Utils.MessageBox(null, "Error: " + err, "Error saat membuat tabel");
- }
- }
- public static int main(string[] args) {
- PhoneBookForm app = new PhoneBookForm();
- return app.run(args);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement