Merge branch 'master' of ssh://pege77@mercury.wipsl.com/var/git/irreco-theme-editor
[irreco.git] / irreco / src / core / irreco.c
blob16da37b92b7ad606517341d1a586c3156a3091cc
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
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.
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 * @mainpage
33 * @par About Irreco
34 * Irreco is a remote control application for Nokia Internet Tablets. With
35 * Irreco and the help of external IR transceiver you can control IR compatible
36 * devices like televisions, DVD players and AV Receivers. Irreco also has a
37 * remote editor which you can use to place buttons to the screen and build the
38 * kind of remote you want to use.
40 * @par Irreco elsewhere
41 * @li Homepage: http://irreco.garage.maemo.org/
42 * @li Garage project: https://garage.maemo.org/projects/irreco/
43 * @li Mailing list: https://garage.maemo.org/mailman/listinfo/irreco-everything
44 * @li Internet Tablet Talk Thread:
45 * http://www.internettablettalk.com/forums/showthread.php?t=15919
46 * @li Maemo Downloads page: http://maemo.org/downloads/product/OS2008/irreco/
48 * @par Documentation
49 * @li Irreco Class documentation can be found in
50 * @htmlonly<a class="el" href="modules.html">Modules</a>@endhtmlonly page.
52 * @par How to implement irreco backend
53 * @li First take a look at @ref IrrecoBackendApi.
54 * @li Then make a copy of @ref PageIrrecoBackendTemplate.
55 * @li And write some code :)
58 /**
59 * @addtogroup Irreco Internal Irreco Classes
64 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
65 /* Prototypes */
66 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
67 void irreco_parse_args(int * argc, char **argv[]);
68 void irreco_process_args(IrrecoData *irreco_data);
69 void irreco_link_bg_image_dir();
73 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
74 /* Main. */
75 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
78 * Popup help message if user doesnt have any remotes.
80 gboolean irreco_startup_help_check(IrrecoData *irreco_data)
82 IRRECO_ENTER
83 if (irreco_string_table_is_empty(irreco_data->irreco_layout_array)) {
85 if (irreco_window_user_create_new_remote(irreco_data,
86 irreco_window_manager_get_gtk_window(
87 irreco_data->window_manager))) {
89 irreco_show_webdb_dlg(irreco_data,
90 irreco_window_manager_get_gtk_window(
91 irreco_data->window_manager));
93 irreco_window_manager_show(irreco_data->window_manager,
94 IRRECO_WINDOW_EDIT);
95 } else {
96 irreco_info_dlg(irreco_window_manager_get_gtk_window(
97 irreco_data->window_manager),
98 _(IRRECO_NO_REMOTE_HELP));
101 IRRECO_RETURN_BOOL(FALSE);
104 int main(int argc, char *argv[])
106 IrrecoData *irreco_data;
107 osso_context_t* osso_context = NULL;
108 IRRECO_ENTER
110 /* Setup gettext. */
111 setlocale(LC_ALL, "");
112 IRRECO_DEBUG("bindtextdomain: \"%s\"\n",
113 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR));
114 IRRECO_DEBUG("bind_textdomain_codeset: \"%s\"\n",
115 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"));
116 IRRECO_DEBUG("textdomain: \"%s\"\n", textdomain(GETTEXT_PACKAGE));
118 /* Create program main data structure & parse arguments. */
119 irreco_data = irreco_data_new();
120 irreco_parse_args(&argc, &argv);
121 IRRECO_DEBUG("LOCALEDIR: %s\n", LOCALEDIR);
122 IRRECO_DEBUG("GETTEXT_PACKAGE: %s\n", GETTEXT_PACKAGE);
124 irreco_process_args(irreco_data);
126 /* Init Web Database*/
127 irreco_webdb_init();
129 /* Initialize the GTK. */
130 g_thread_init(NULL);
131 /*gdk_threads_init();*/
132 gtk_init(&argc, &argv);
134 /* Initialize libosso and maemo dbus thingy. */
135 osso_context = osso_initialize(PACKAGE_DBUS_NAME, PACKAGE_VERSION,
136 TRUE, NULL);
137 if (osso_context == NULL) {
138 IRRECO_PRINTF("Failed to init LibOSSO\n");
139 return EXIT_FAILURE;
142 /* Init UI. */
143 g_set_application_name(IRRECO_APP_NAME_SHORT);
144 irreco_window_manager_show(irreco_data->window_manager,
145 IRRECO_WINDOW_USER);
147 /* Load plugins. */
149 IrrecoBackendManager* manager = irreco_data->irreco_backend_manager;
150 irreco_backend_manager_load_lib_dir(manager, IRRECO_BACKEND_DIR);
151 irreco_config_read_backends(manager);
152 irreco_backend_manager_print(manager);
153 irreco_backend_manager_read_from_confs(manager);
154 irreco_backend_manager_get_devcmd_lists(manager);
157 /* Read layouts. */
158 irreco_data->theme_manager = irreco_theme_manager_new(irreco_data);
159 irreco_config_read_layouts(irreco_data);
161 /* Create link to background image dir. */
162 irreco_link_bg_image_dir();
164 /* Begin the main application. */
165 irreco_window_manager_set_layout(irreco_data->window_manager,
166 irreco_config_read_active_layout(
167 irreco_data));
168 g_idle_add(G_SOURCEFUNC(irreco_startup_help_check), irreco_data);
169 gtk_main();
171 /* Save active layout. */
172 if (irreco_data->window_manager->current_layout != NULL) {
173 irreco_config_save_active_layout(
174 irreco_data->window_manager->current_layout);
177 /* Exit */
178 irreco_data_free(irreco_data);
179 osso_deinitialize(osso_context);
180 irreco_webdb_finalize();
181 IRRECO_RETURN_INT(0);
186 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
187 /* Command line argument parsing. */
188 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
190 static gchar** style_dir_array = NULL;
191 static gchar** style_file_array = NULL;
192 static GOptionEntry irreco_option_entries[] = {
193 { "button-style-dir", 'B', 0, G_OPTION_ARG_FILENAME_ARRAY,
194 &style_dir_array, "Read button styles files from directory", "DIR" },
195 /*{ "button-style-file", 'b', 0, G_OPTION_ARG_FILENAME_ARRAY,
196 &style_file_array, "Read button style from file.", "FILE" },*/
197 { "debug", 'd', 0, G_OPTION_ARG_INT, &IRRECO_DEBUG_LEVEL_VAR,
198 "Debugging ouput level", "N" },
199 { NULL }
202 void irreco_parse_args(int * argc, char **argv[])
204 gchar* help_title;
205 GError *error = NULL;
206 GOptionContext *context;
207 IRRECO_ENTER
209 /* Format help title. */
210 help_title = g_strjoin("", "- ", IRRECO_APP_NAME_LONG,
211 " v", VERSION, NULL);
212 context = g_option_context_new(help_title);
213 g_free(help_title);
215 /* Parse arguments. */
216 g_option_context_add_main_entries(context, irreco_option_entries,
217 GETTEXT_PACKAGE);
218 g_option_context_add_group(context, gtk_get_option_group(TRUE));
219 g_option_context_parse(context, argc, argv, &error);
220 if (irreco_gerror_check_print(&error)) {
221 exit(1);
223 g_option_context_free(context);
224 IRRECO_RETURN
227 void irreco_process_args(IrrecoData *irreco_data)
229 guint array_pos;
230 gboolean file_error = FALSE;
231 IRRECO_ENTER
233 /* Check file filetypes. */
234 if (style_dir_array != NULL) {
235 for (array_pos = g_strv_length(style_dir_array); array_pos--;) {
236 if (!irreco_is_dir(style_dir_array[array_pos])) {
237 IRRECO_ERROR("\"%s\" is not a directory.\n",
238 style_dir_array[array_pos]);
239 file_error = TRUE;
243 if (style_file_array != NULL) {
244 for (array_pos = g_strv_length(style_file_array); array_pos--;) {
245 if (!irreco_is_file(style_file_array[array_pos])) {
246 IRRECO_ERROR("\"%s\" is not a file.\n",
247 style_file_array[array_pos]);
248 file_error = TRUE;
252 if (file_error) {
253 exit(1);
256 /* Read button configurations. */
257 if (style_dir_array != NULL) {
258 for (array_pos = g_strv_length(style_dir_array); array_pos--;) {
259 IRRECO_PRINTF("Reading button styles from \"%s\".\n",
260 style_dir_array[array_pos]);
261 /*irreco_config_read_themes_from_dir(
262 irreco_data, style_dir_array[array_pos]);*/
263 irreco_theme_manager_read_themes_from_dir(
264 irreco_data->theme_manager,
265 style_dir_array[array_pos]);
267 g_strfreev(style_dir_array);
268 style_dir_array = NULL;
271 IRRECO_RETURN
275 * Default Irreco background images are installed to somewhere under /usr/lib.
276 * That location is not normally accessible with the Maemo file selection
277 * dialog. So in order to work around that, we create a symlink into
278 * $HOME/MyDocs/.images/ which points to that directory.
280 void irreco_link_bg_image_dir()
282 const gchar *oldpwd;
283 const gchar link_name[] = "Irreco default backgrounds";
284 IRRECO_ENTER
286 oldpwd = getenv("PWD");
287 if (chdir(getenv("HOME")) != 0 || chdir("MyDocs/.images/") != 0) {
288 IRRECO_ERROR("Could not chdir into "
289 "\"$HOME/MyDocs/.images/\".\n");
290 IRRECO_RETURN
293 if (irreco_file_exists(link_name)) {
294 IRRECO_PRINTF("File \"%s\" already exists.\n", link_name);
295 } else if(symlink(IRRECO_BG_IMAGE_DIR, link_name) == 0) {
296 IRRECO_PRINTF("Created link from \"%s\" as \"%s\".\n",
297 link_name, IRRECO_BG_IMAGE_DIR);
298 } else {
299 IRRECO_ERROR("Could not make a link from \"%s\" to \"%s\".\n",
300 link_name, IRRECO_BG_IMAGE_DIR);
303 chdir(oldpwd);
304 IRRECO_RETURN