ThemeManager added
[irreco.git] / irreco / trunk / src / core / irreco.c
blob2754aa25cff9d3aee5a834228633efe8b4c7573b
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 #include "irreco_theme_manager.h"
33 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
34 /* Prototypes */
35 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
36 void irreco_parse_args(int * argc, char **argv[]);
37 void irreco_process_args(IrrecoData *irreco_data);
38 void irreco_link_bg_image_dir();
42 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
43 /* Main. */
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
47 * Popup help message if user doesnt have any remotes.
49 gboolean irreco_startup_help_check(IrrecoData *irreco_data)
51 IRRECO_ENTER
52 if (irreco_string_table_is_empty(irreco_data->irreco_layout_array)) {
54 if (irreco_window_user_create_new_remote(irreco_data,
55 irreco_window_manager_get_gtk_window(
56 irreco_data->window_manager))) {
58 irreco_show_webdb_dlg(irreco_data,
59 irreco_window_manager_get_gtk_window(
60 irreco_data->window_manager));
62 irreco_window_manager_show(irreco_data->window_manager,
63 IRRECO_WINDOW_EDIT);
64 } else {
65 irreco_info_dlg(irreco_window_manager_get_gtk_window(
66 irreco_data->window_manager),
67 _(IRRECO_NO_REMOTE_HELP));
70 IRRECO_RETURN_BOOL(FALSE);
73 int main(int argc, char *argv[])
75 IrrecoData *irreco_data;
76 osso_context_t* osso_context = NULL;
77 IRRECO_ENTER
81 gboolean success;
82 IrrecoRetryLoop *loop;
83 loop = irreco_retry_loop_new(IRRECO_SECONDS_TO_USEC(0.1),
84 IRRECO_SECONDS_TO_USEC(3));
86 IRRECO_RETRY_LOOP_START(loop)
87 success = FALSE;
88 if (success) break;
89 IRRECO_RETRY_LOOP_END(loop)
91 if (success) {
92 IRRECO_PRINTF("Success!!\n");
93 } else {
94 IRRECO_PRINTF("Failure!!\n");
97 irreco_retry_loop_free(loop);
101 #if 0
105 gint i = 0;
106 IrrecoStringTable *category_list;
107 IrrecoWebdbCache *cache;
109 cache = irreco_webdb_cache_new();
110 irreco_webdb_cache_get_categories(cache, &category_list);
112 irreco_string_table_print(category_list);
115 IRRECO_STRING_TABLE_FOREACH(category_list, key, void *, data)
116 IRRECO_PRINTF("%u: %p, %s\n", ++i, data, key);
117 IRRECO_STRING_TABLE_FOREACH_END
121 gint sum = 0;
122 if (irreco_webdb_sum(1, 2, &sum)) {
123 IRRECO_PRINTF("SUM: %i\n", sum);
129 irreco_webdb_finalize();*/
130 IRRECO_PRINTF(" Successfully multifailed\n");
133 return 0;
134 #endif
136 /* Setup gettext. */
137 setlocale(LC_ALL, "");
138 IRRECO_DEBUG("bindtextdomain: \"%s\"\n",
139 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR));
140 IRRECO_DEBUG("bind_textdomain_codeset: \"%s\"\n",
141 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"));
142 IRRECO_DEBUG("textdomain: \"%s\"\n",
143 textdomain(GETTEXT_PACKAGE));
145 /* Create program main data structure & parse arguments. */
146 irreco_data = irreco_data_new();
147 irreco_parse_args(&argc, &argv);
148 IRRECO_DEBUG("LOCALEDIR: %s\n", LOCALEDIR);
149 IRRECO_DEBUG("GETTEXT_PACKAGE: %s\n", GETTEXT_PACKAGE);
151 /* Read styles. */
152 #if 0
154 IrrecoThemeManager *theme_manager;
155 theme_manager = irreco_theme_manager_new(irreco_data, IRRECO_THEME_DIR);
156 irreco_theme_manager_print(theme_manager);
157 IRRECO_PAUSE
159 /*return 0;*/
161 #endif
163 #if 1
164 IRRECO_DEBUG("IRRECO_THEME_DIR: %s\n", IRRECO_THEME_DIR);
165 irreco_config_read_themes_from_dir(irreco_data, IRRECO_THEME_DIR);
166 irreco_config_read_themes_from_dir(irreco_data, "/media/mmc1/irreco");
167 irreco_config_read_themes_from_dir(irreco_data, "/media/mmc2/irreco");
169 #endif
171 irreco_config_read_styles_from_dir(irreco_data, IRRECO_BUTTON_STYLE_DIR);
172 irreco_config_read_styles_from_dir(irreco_data, "/media/mmc1/irreco");
173 irreco_config_read_styles_from_dir(irreco_data, "/media/mmc2/irreco");
175 irreco_process_args(irreco_data);
176 irreco_string_table_sort_abc(irreco_data->button_style_array);
178 /* Init Web Database*/
179 irreco_webdb_init();
181 /* Initialize the GTK. */
182 g_thread_init(NULL);
183 /*gdk_threads_init();*/
184 gtk_init(&argc, &argv);
186 /* Initialize libosso and maemo dbus thingy. */
187 osso_context = osso_initialize(PACKAGE_DBUS_NAME, PACKAGE_VERSION,
188 TRUE, NULL);
189 if (osso_context == NULL) {
190 IRRECO_PRINTF("Failed to init LibOSSO\n");
191 return EXIT_FAILURE;
194 /* Init UI. */
195 g_set_application_name(IRRECO_APP_NAME_SHORT);
196 irreco_window_manager_show(irreco_data->window_manager,
197 IRRECO_WINDOW_USER);
199 /* Load plugins. */
201 IrrecoBackendManager* manager = irreco_data->irreco_backend_manager;
202 irreco_backend_manager_load_lib_dir(manager, IRRECO_BACKEND_DIR);
203 irreco_config_read_backends(manager);
204 irreco_backend_manager_print(manager);
205 irreco_backend_manager_read_from_confs(manager);
206 irreco_backend_manager_get_devcmd_lists(manager);
209 /* Read layouts. */
210 irreco_config_read_layouts(irreco_data);
212 /* Create link to background image dir. */
213 irreco_link_bg_image_dir();
215 /* Begin the main application. */
216 irreco_window_manager_set_layout(irreco_data->window_manager,
217 irreco_config_read_active_layout(
218 irreco_data));
219 g_idle_add(G_SOURCEFUNC(irreco_startup_help_check), irreco_data);
220 gtk_main();
222 /* Save active layout. */
223 if (irreco_data->window_manager->current_layout != NULL) {
224 irreco_config_save_active_layout(
225 irreco_data->window_manager->current_layout);
228 /* Exit */
229 irreco_data_free(irreco_data);
230 osso_deinitialize(osso_context);
231 irreco_webdb_finalize();
232 IRRECO_RETURN_INT(0);
237 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
238 /* Command line argument parsing. */
239 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
241 static gchar** style_dir_array = NULL;
242 static gchar** style_file_array = NULL;
243 static GOptionEntry irreco_option_entries[] = {
244 { "button-style-dir", 'B', 0, G_OPTION_ARG_FILENAME_ARRAY,
245 &style_dir_array, "Read button styles files from directory", "DIR" },
246 { "button-style-file", 'b', 0, G_OPTION_ARG_FILENAME_ARRAY,
247 &style_file_array, "Read button style from file.", "FILE" },
248 { "debug", 'd', 0, G_OPTION_ARG_INT, &IRRECO_DEBUG_LEVEL_VAR,
249 "Debugging ouput level", "N" },
250 { NULL }
253 void irreco_parse_args(int * argc, char **argv[])
255 gchar* help_title;
256 GError *error = NULL;
257 GOptionContext *context;
258 IRRECO_ENTER
260 /* Format help title. */
261 help_title = g_strjoin("", "- ", IRRECO_APP_NAME_LONG,
262 " v", VERSION, NULL);
263 context = g_option_context_new(help_title);
264 g_free(help_title);
266 /* Parse arguments. */
267 g_option_context_add_main_entries(context, irreco_option_entries,
268 GETTEXT_PACKAGE);
269 g_option_context_add_group(context, gtk_get_option_group(TRUE));
270 g_option_context_parse(context, argc, argv, &error);
271 if (irreco_gerror_check_print(&error)) {
272 exit(1);
274 g_option_context_free(context);
275 IRRECO_RETURN
278 void irreco_process_args(IrrecoData *irreco_data)
280 guint array_pos;
281 gboolean file_error = FALSE;
282 IRRECO_ENTER
284 /* Check file filetypes. */
285 if (style_dir_array != NULL) {
286 for (array_pos = g_strv_length(style_dir_array); array_pos--;) {
287 if (!irreco_is_dir(style_dir_array[array_pos])) {
288 IRRECO_ERROR("\"%s\" is not a directory.\n",
289 style_dir_array[array_pos]);
290 file_error = TRUE;
294 if (style_file_array != NULL) {
295 for (array_pos = g_strv_length(style_file_array); array_pos--;) {
296 if (!irreco_is_file(style_file_array[array_pos])) {
297 IRRECO_ERROR("\"%s\" is not a file.\n",
298 style_file_array[array_pos]);
299 file_error = TRUE;
303 if (file_error) {
304 exit(1);
307 /* Read button configurations. */
308 if (style_dir_array != NULL) {
309 for (array_pos = g_strv_length(style_dir_array); array_pos--;) {
310 IRRECO_PRINTF("Reading button styles from \"%s\".\n",
311 style_dir_array[array_pos]);
312 irreco_config_read_themes_from_dir(
313 irreco_data, style_dir_array[array_pos]);
315 g_strfreev(style_dir_array);
316 style_dir_array = NULL;
318 if (style_file_array != NULL) {
319 for (array_pos = g_strv_length(style_file_array); array_pos--;){
320 IRRECO_PRINTF("\nReading button style from \"%s\".\n",
321 style_file_array[array_pos]);
322 irreco_config_read_style_from_file(
323 irreco_data, style_file_array[array_pos]);
325 g_strfreev(style_file_array);
326 style_file_array = NULL;
329 IRRECO_RETURN
333 * Default Irreco background images are installed to somewhere under /usr/lib.
334 * That location is not normally accessible with the Maemo file selection
335 * dialog. So in order to work around that, we create a symlink into
336 * $HOME/MyDocs/.images/ which points to that directory.
338 void irreco_link_bg_image_dir()
340 const gchar *oldpwd;
341 const gchar link_name[] = "Irreco default backgrounds";
342 IRRECO_ENTER
344 oldpwd = getenv("PWD");
345 if (chdir(getenv("HOME")) != 0 || chdir("MyDocs/.images/") != 0) {
346 IRRECO_ERROR("Could not chdir into "
347 "\"$HOME/MyDocs/.images/\".\n");
348 IRRECO_RETURN
351 if (irreco_file_exists(link_name)) {
352 IRRECO_PRINTF("File \"%s\" already exists.\n", link_name);
353 } else if(symlink(IRRECO_BG_IMAGE_DIR, link_name) == 0) {
354 IRRECO_PRINTF("Created link from \"%s\" as \"%s\".\n",
355 link_name, IRRECO_BG_IMAGE_DIR);
356 } else {
357 IRRECO_ERROR("Could not make a link from \"%s\" to \"%s\".\n",
358 link_name, IRRECO_BG_IMAGE_DIR);
361 chdir(oldpwd);
362 IRRECO_RETURN