Maemopad+ 0.37 released
[maemopadplus.git] / src / main.c
blob764d43d452cb9aec79fdd1fd4015cf78f7c645a3
1 /*
2 * This file is part of maemopad+
5 * This software is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This software is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this software; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
22 #include <ui/interface.h>
23 #include <ui/callbacks.h>
24 #include <string.h>
26 #include <gtk/gtkmain.h>
27 #include <hildon/hildon-program.h>
28 #include <hildon/hildon-note.h>
29 #include <libintl.h>
30 #include <locale.h>
31 #include <libosso.h>
33 #define _(String) gettext(String)
35 #include <config.h>
36 #include <appdata.h>
38 gint
39 dbus_callback (const gchar *interface, const gchar *method,
40 GArray *arguments, gpointer data,
41 osso_rpc_t *retval)
43 printf ("maemopad+ dbus: %s, %s\n", interface, method);
45 if (!strcmp (method, "top_application"))
46 gtk_window_present (GTK_WINDOW (data));
48 retval->type = DBUS_TYPE_INVALID;
49 return OSSO_OK;
52 int main(int argc, char *argv[])
54 AppData *data;
55 HildonProgram *app;
56 MainView *main_view;
58 /* Initialise the locale stuff */
59 setlocale(LC_ALL, "");
60 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
61 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
62 textdomain(GETTEXT_PACKAGE);
64 /* Init the gtk - must be called before any hildon stuff */
65 gtk_init(&argc, &argv);
67 /* Create the hildon application and setup the title */
68 app = HILDON_PROGRAM ( hildon_program_get_instance () );
69 g_set_application_name ( _("Maemopad+") );
71 /* Create the data and views for our application */
72 data = g_new0(AppData, 1);
73 data->osso = osso_initialize("maemopadplus", VERSION, FALSE, NULL);
74 g_assert(data->osso);
75 data->app = app;
76 main_view = interface_main_view_new(data);
77 hildon_program_add_window( data->app, data->main_view );
79 osso_return_t ossores;
81 /* Add handler for Exit D-BUS messages */
82 ossores = osso_application_set_exit_cb(data->osso, exit_event_handler, (gpointer) main_view);
83 if (ossores != OSSO_OK)
85 g_print("Error setting exit callback (%d)\n", ossores);
86 return OSSO_ERROR;
89 /* Add handler for hardware D-BUS messages */
90 ossores = osso_hw_set_event_cb(data->osso, NULL, hw_event_handler, (gpointer) main_view);
91 if (ossores != OSSO_OK)
93 g_print("Error setting HW state callback (%d)\n", ossores);
94 return OSSO_ERROR;
97 ossores = osso_rpc_set_default_cb_f (data->osso, dbus_callback, data->main_view);
98 if (ossores != OSSO_OK)
100 fprintf (stderr, "osso_rpc_set_default_cb_f failed: %d.\n", ossores);
101 return 1;
104 data->gconf = gconf_client_get_default();
105 gchar *gcfilename = gconf_client_get_string(data->gconf, "/apps/maemopadPlus/general/lastdocument", NULL);
107 gtk_widget_show(GTK_WIDGET(data->main_view));
109 gtk_widget_hide(main_view->scrolledwindow);
111 gtk_widget_show(sketchwidget_get_mainwidget(main_view->sk));
112 gtk_widget_realize(sketchwidget_get_drawingarea(main_view->sk));
114 /* gtk_widget_map(sketchwidget_get_drawingarea(main_view->sk));
116 gtk_widget_hide(sketchwidget_get_mainwidget(main_view->sk));
118 if (gcfilename)
120 open_file(gcfilename, main_view);
121 g_free(gcfilename);
126 do /*FIXME:bad code */
128 if (main_view->file_name == NULL)
130 if (interface_create_new_file(main_view) == CONFRESP_YES)
131 callback_file_new(NULL, main_view);
132 else
133 callback_file_open(NULL, main_view);
135 if (main_view->file_name == NULL)
137 HildonNote *hn = NULL;
138 gint response;
140 hn = HILDON_NOTE(hildon_note_new_confirmation_add_buttons(GTK_WINDOW(main_view->data->main_view), _("Exit?"), _("Yes"), CONFRESP_YES, _("No"), CONFRESP_NO, NULL, NULL));
141 response = gtk_dialog_run(GTK_DIALOG(hn));
142 gtk_widget_destroy(GTK_WIDGET(hn));
143 if (response == TRUE)
144 break;
146 } while(main_view->file_name == NULL);
147 if (main_view->file_name == NULL)
148 break;
150 /* Begin the main app */
151 gtk_widget_show(GTK_WIDGET(data->main_view));
152 gtk_main();
154 if (main_view->file_name)
156 printf("exit:*%s*\n", main_view->file_name);
157 gconf_client_set_string(data->gconf, "/apps/maemopadPlus/general/lastdocument", main_view->file_name, NULL);
159 } while(FALSE);
161 /* Clean up */
162 if (main_view->db)
163 sqlite3_close(main_view->db);
165 sketchwidget_destroy(main_view->sk);
166 interface_main_view_destroy(main_view);
168 osso_deinitialize(data->osso);
169 g_free(data);
171 return 0;