Improved commentary.
[irreco.git] / irreco / src / core / irreco.c
blob5530f0855f4b93b621bc021588053f5246bf8058
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco.h"
21 #include "irreco_data.h"
22 #include "irreco_config.h"
23 #include "irreco_window_user.h"
24 #include "irreco_webdb_dlg.h"
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <libosso.h>
27 #include <irreco_webdb.h>
28 #include <irreco_retry_loop.h>
30 /**
31 * @defgroup Irreco Internal Irreco Classes
36 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
37 /* Prototypes */
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 void irreco_parse_args(int * argc, char **argv[]);
40 void irreco_process_args(IrrecoData *irreco_data);
41 void irreco_link_bg_image_dir();
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 /* Main. */
47 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
50 * Popup help message if user doesnt have any remotes.
52 gboolean irreco_startup_help_check(IrrecoData *irreco_data)
54 IRRECO_ENTER
55 if (irreco_string_table_is_empty(irreco_data->irreco_layout_array)) {
57 if (irreco_window_user_create_new_remote(irreco_data,
58 irreco_window_manager_get_gtk_window(
59 irreco_data->window_manager))) {
61 irreco_show_webdb_dlg(irreco_data,
62 irreco_window_manager_get_gtk_window(
63 irreco_data->window_manager));
65 irreco_window_manager_show(irreco_data->window_manager,
66 IRRECO_WINDOW_EDIT);
67 } else {
68 irreco_info_dlg(irreco_window_manager_get_gtk_window(
69 irreco_data->window_manager),
70 _(IRRECO_NO_REMOTE_HELP));
73 IRRECO_RETURN_BOOL(FALSE);
76 int main(int argc, char *argv[])
78 IrrecoData *irreco_data;
79 osso_context_t* osso_context = NULL;
80 IRRECO_ENTER
82 /* Setup gettext. */
83 setlocale(LC_ALL, "");
84 IRRECO_DEBUG("bindtextdomain: \"%s\"\n",
85 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR));
86 IRRECO_DEBUG("bind_textdomain_codeset: \"%s\"\n",
87 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"));
88 IRRECO_DEBUG("textdomain: \"%s\"\n", textdomain(GETTEXT_PACKAGE));
90 /* Create program main data structure & parse arguments. */
91 irreco_data = irreco_data_new();
92 irreco_parse_args(&argc, &argv);
93 IRRECO_DEBUG("LOCALEDIR: %s\n", LOCALEDIR);
94 IRRECO_DEBUG("GETTEXT_PACKAGE: %s\n", GETTEXT_PACKAGE);
96 irreco_process_args(irreco_data);
98 /* Init Web Database*/
99 irreco_webdb_init();
101 /* Initialize the GTK. */
102 g_thread_init(NULL);
103 /*gdk_threads_init();*/
104 gtk_init(&argc, &argv);
106 /* Initialize libosso and maemo dbus thingy. */
107 osso_context = osso_initialize(PACKAGE_DBUS_NAME, PACKAGE_VERSION,
108 TRUE, NULL);
109 if (osso_context == NULL) {
110 IRRECO_PRINTF("Failed to init LibOSSO\n");
111 return EXIT_FAILURE;
114 /* Init UI. */
115 g_set_application_name(IRRECO_APP_NAME_SHORT);
116 irreco_window_manager_show(irreco_data->window_manager,
117 IRRECO_WINDOW_USER);
119 /* Load plugins. */
121 IrrecoBackendManager* manager = irreco_data->irreco_backend_manager;
122 irreco_backend_manager_load_lib_dir(manager, IRRECO_BACKEND_DIR);
123 irreco_config_read_backends(manager);
124 irreco_backend_manager_print(manager);
125 irreco_backend_manager_read_from_confs(manager);
126 irreco_backend_manager_get_devcmd_lists(manager);
129 /* Read layouts. */
130 irreco_data->theme_manager = irreco_theme_manager_new(irreco_data);
131 irreco_config_read_layouts(irreco_data);
133 /* Create link to background image dir. */
134 irreco_link_bg_image_dir();
136 /* Begin the main application. */
137 irreco_window_manager_set_layout(irreco_data->window_manager,
138 irreco_config_read_active_layout(
139 irreco_data));
140 g_idle_add(G_SOURCEFUNC(irreco_startup_help_check), irreco_data);
141 gtk_main();
143 /* Save active layout. */
144 if (irreco_data->window_manager->current_layout != NULL) {
145 irreco_config_save_active_layout(
146 irreco_data->window_manager->current_layout);
149 /* Exit */
150 irreco_data_free(irreco_data);
151 osso_deinitialize(osso_context);
152 irreco_webdb_finalize();
153 IRRECO_RETURN_INT(0);
158 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
159 /* Command line argument parsing. */
160 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
162 static gchar** style_dir_array = NULL;
163 static gchar** style_file_array = NULL;
164 static GOptionEntry irreco_option_entries[] = {
165 { "button-style-dir", 'B', 0, G_OPTION_ARG_FILENAME_ARRAY,
166 &style_dir_array, "Read button styles files from directory", "DIR" },
167 /*{ "button-style-file", 'b', 0, G_OPTION_ARG_FILENAME_ARRAY,
168 &style_file_array, "Read button style from file.", "FILE" },*/
169 { "debug", 'd', 0, G_OPTION_ARG_INT, &IRRECO_DEBUG_LEVEL_VAR,
170 "Debugging ouput level", "N" },
171 { NULL }
174 void irreco_parse_args(int * argc, char **argv[])
176 gchar* help_title;
177 GError *error = NULL;
178 GOptionContext *context;
179 IRRECO_ENTER
181 /* Format help title. */
182 help_title = g_strjoin("", "- ", IRRECO_APP_NAME_LONG,
183 " v", VERSION, NULL);
184 context = g_option_context_new(help_title);
185 g_free(help_title);
187 /* Parse arguments. */
188 g_option_context_add_main_entries(context, irreco_option_entries,
189 GETTEXT_PACKAGE);
190 g_option_context_add_group(context, gtk_get_option_group(TRUE));
191 g_option_context_parse(context, argc, argv, &error);
192 if (irreco_gerror_check_print(&error)) {
193 exit(1);
195 g_option_context_free(context);
196 IRRECO_RETURN
199 void irreco_process_args(IrrecoData *irreco_data)
201 guint array_pos;
202 gboolean file_error = FALSE;
203 IRRECO_ENTER
205 /* Check file filetypes. */
206 if (style_dir_array != NULL) {
207 for (array_pos = g_strv_length(style_dir_array); array_pos--;) {
208 if (!irreco_is_dir(style_dir_array[array_pos])) {
209 IRRECO_ERROR("\"%s\" is not a directory.\n",
210 style_dir_array[array_pos]);
211 file_error = TRUE;
215 if (style_file_array != NULL) {
216 for (array_pos = g_strv_length(style_file_array); array_pos--;) {
217 if (!irreco_is_file(style_file_array[array_pos])) {
218 IRRECO_ERROR("\"%s\" is not a file.\n",
219 style_file_array[array_pos]);
220 file_error = TRUE;
224 if (file_error) {
225 exit(1);
228 /* Read button configurations. */
229 if (style_dir_array != NULL) {
230 for (array_pos = g_strv_length(style_dir_array); array_pos--;) {
231 IRRECO_PRINTF("Reading button styles from \"%s\".\n",
232 style_dir_array[array_pos]);
233 /*irreco_config_read_themes_from_dir(
234 irreco_data, style_dir_array[array_pos]);*/
235 irreco_theme_manager_read_themes_from_dir(
236 irreco_data->theme_manager,
237 style_dir_array[array_pos]);
239 g_strfreev(style_dir_array);
240 style_dir_array = NULL;
243 IRRECO_RETURN
247 * Default Irreco background images are installed to somewhere under /usr/lib.
248 * That location is not normally accessible with the Maemo file selection
249 * dialog. So in order to work around that, we create a symlink into
250 * $HOME/MyDocs/.images/ which points to that directory.
252 void irreco_link_bg_image_dir()
254 const gchar *oldpwd;
255 const gchar link_name[] = "Irreco default backgrounds";
256 IRRECO_ENTER
258 oldpwd = getenv("PWD");
259 if (chdir(getenv("HOME")) != 0 || chdir("MyDocs/.images/") != 0) {
260 IRRECO_ERROR("Could not chdir into "
261 "\"$HOME/MyDocs/.images/\".\n");
262 IRRECO_RETURN
265 if (irreco_file_exists(link_name)) {
266 IRRECO_PRINTF("File \"%s\" already exists.\n", link_name);
267 } else if(symlink(IRRECO_BG_IMAGE_DIR, link_name) == 0) {
268 IRRECO_PRINTF("Created link from \"%s\" as \"%s\".\n",
269 link_name, IRRECO_BG_IMAGE_DIR);
270 } else {
271 IRRECO_ERROR("Could not make a link from \"%s\" to \"%s\".\n",
272 link_name, IRRECO_BG_IMAGE_DIR);
275 chdir(oldpwd);
276 IRRECO_RETURN