PageRenderTime 77ms CodeModel.GetById 21ms app.highlight 29ms RepoModel.GetById 15ms app.codeStats 0ms

/apps/desktop/main.c

http://ftk.googlecode.com/
C | 359 lines | 264 code | 66 blank | 29 comment | 26 complexity | e9f12708fab91c0a25a181b01dd7dcba MD5 | raw file
  1/*
  2 * File: main.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   desktop main
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2009-11-29 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk.h"
 33#include <time.h>
 34#include "ftk_xul.h"
 35#include "ftk_util.h"
 36#include "app_info.h"
 37#include "ftk_tester.h"
 38#include "vnc_service.h"
 39
 40typedef struct _FtkDesktop
 41{
 42	int is_horizonal;
 43	FtkWidget*      applist_win;
 44	AppInfoManager* app_manager;
 45	FtkIconCache*   icon_cache;
 46}FtkDesktop;
 47
 48static FtkDesktop g_desktop;
 49
 50static Ret desktop_on_button_close_applist_clicked(void* ctx, void* obj)
 51{
 52	ftk_widget_show(ctx, 0);
 53
 54	return RET_OK;
 55}
 56
 57static const char* desktop_translate_text(void* ctx, const char* text)
 58{
 59	return _(text);
 60}
 61
 62static const char* desktop_translate_path(const char* path, char out_path[FTK_MAX_PATH+1])
 63{
 64	ftk_snprintf(out_path, FTK_MAX_PATH, "%s/desktop/%s", FTK_ROOT_DIR, path);
 65	ftk_normalize_path(out_path);
 66
 67	ftk_logd("%s: %s --> %s\n", __func__, path, out_path);
 68
 69	return out_path;
 70}
 71
 72static FtkWidget* desktop_load_xul(const char* filename)
 73{
 74	FtkXulCallbacks callbacks = {0};
 75	char path[FTK_MAX_PATH+1] = {0};
 76	
 77	callbacks.ctx = g_desktop.icon_cache;
 78	callbacks.translate_text = desktop_translate_text;
 79	callbacks.load_image = (FtkXulLoadImage)ftk_icon_cache_load;
 80
 81	desktop_translate_path(filename, path);
 82
 83	return ftk_xul_load_file(path, &callbacks);
 84}
 85
 86static Ret applist_on_item_clicked(void* ctx, void* obj)
 87{
 88	FtkIconViewItem* item = obj;
 89	AppInfo* info = item->user_data;
 90
 91	ftk_app_run(info->app, 0, NULL);
 92
 93	ftk_logd("%s: %s: user_data=%d\n", __func__, item->text, (int)item->user_data);
 94
 95	return RET_OK;
 96}
 97
 98static Ret desktop_on_button_open_applist_clicked(void* ctx, void* obj)
 99{
100	size_t i = 0;
101	size_t n = 0;
102	size_t item_size = 100;
103	FtkWidget* win = NULL;
104	AppInfo* app_info = NULL;
105	FtkWidget* button = NULL;
106	FtkIconViewItem item;
107	FtkWidget* icon_view = NULL;
108	
109	if(g_desktop.applist_win != NULL)
110	{
111		ftk_widget_show_all(g_desktop.applist_win, 1);
112
113		return RET_OK;
114	}
115
116	win = desktop_load_xul(g_desktop.is_horizonal ? "xul/appview-h.xul" : "xul/appview-v.xul"); 
117	
118	button = ftk_widget_lookup(win, 100);
119	ftk_button_set_clicked_listener(button, desktop_on_button_close_applist_clicked, win);
120
121	icon_view = ftk_widget_lookup(win, 99);
122
123	item_size = 6 * ftk_widget_get_font_size(icon_view);
124	if(ftk_widget_width(icon_view) < 2 * item_size)
125	{
126		item_size = (ftk_widget_width(icon_view) - 10) / 2;
127	}
128
129	ftk_icon_view_set_item_size(icon_view, item_size);
130	ftk_icon_view_set_clicked_listener(icon_view, applist_on_item_clicked, win);
131	n = app_info_manager_get_count(g_desktop.app_manager);
132	
133	for(i = 0; i < n; i++)
134	{
135		app_info_manager_get(g_desktop.app_manager, i, &app_info);
136		
137		item.icon = ftk_app_get_icon(app_info->app);
138		item.user_data = app_info;
139		item.text = (char*)ftk_app_get_name(app_info->app);
140		if(item.text == NULL)
141		{
142			item.text = app_info->name;
143		}
144		ftk_icon_view_add(icon_view, &item);
145	}
146
147	g_desktop.applist_win = win;
148
149	return RET_OK;
150}
151
152#define IDC_TIME_ITEM 2000
153
154static Ret desktop_update_time(void* ctx)
155{
156	char text[10] = {0};
157	time_t now = time(0);
158	FtkWidget* item = NULL;
159	FtkWidget* panel = NULL;
160	FtkWidget* win = ctx;
161	FtkWidget* image = NULL;
162	FtkBitmap* bitmap = NULL;
163	char filename[FTK_MAX_PATH] = {0};
164	struct tm* t = localtime(&now);
165
166	panel = ftk_default_status_panel();
167	if(panel == NULL)
168	{
169		return RET_REMOVE;
170	}
171
172	ftk_snprintf(text, sizeof(text)-1, "%d:%02d", t->tm_hour, t->tm_min);
173	item = ftk_widget_lookup(panel, IDC_TIME_ITEM);
174	ftk_widget_set_text(item, text);
175	ftk_logd("%s\n", __func__);
176	
177	image = ftk_widget_lookup(win, 1);
178	ftk_snprintf(filename, sizeof(filename)-1, "icons/%d.png", t->tm_hour/10);
179	bitmap = ftk_icon_cache_load(g_desktop.icon_cache, filename);
180	ftk_image_set_image(image, bitmap);
181	
182	image = ftk_widget_lookup(win, 2);
183	ftk_snprintf(filename, sizeof(filename)-1, "icons/%d.png", t->tm_hour%10);
184	bitmap = ftk_icon_cache_load(g_desktop.icon_cache, filename);
185	ftk_image_set_image(image, bitmap);
186
187	image = ftk_widget_lookup(win, 4);
188	ftk_snprintf(filename, sizeof(filename)-1, "icons/%d.png", t->tm_min/10);
189	bitmap = ftk_icon_cache_load(g_desktop.icon_cache, filename);
190	ftk_image_set_image(image, bitmap);
191	
192	image = ftk_widget_lookup(win, 5);
193	ftk_snprintf(filename, sizeof(filename)-1, "icons/%d.png", t->tm_min%10);
194	bitmap = ftk_icon_cache_load(g_desktop.icon_cache, filename);
195	ftk_image_set_image(image, bitmap);
196
197	return RET_OK;
198}
199
200static Ret desktop_on_shutdown(void* ctx, void* obj)
201{
202#ifdef USE_VNC
203	if(ftk_display_vnc_is_active())
204	{
205		ftk_display_vnc_quit();
206	}
207#endif
208	ftk_quit();
209
210	return RET_OK;
211}
212
213#ifdef USE_VNC
214static Ret desktop_on_vnc(void* ctx, void* obj)
215{
216	if(ftk_display_vnc_is_active())
217	{
218		ftk_display_vnc_stop();
219	}
220	else
221	{
222		ftk_display_vnc_start();
223	}
224	
225	return RET_OK;
226}
227#endif
228
229static Ret desktop_on_prepare_options_menu(void* ctx, FtkWidget* menu_panel)
230{
231	FtkWidget* item = ftk_menu_item_create(menu_panel);
232	ftk_widget_set_text(item, _("Shutdown"));
233	ftk_menu_item_set_clicked_listener(item, desktop_on_shutdown, ctx);
234	ftk_widget_show(item, 1);
235
236#ifdef USE_VNC
237	item = ftk_menu_item_create(menu_panel);
238	if(ftk_display_vnc_is_active())
239	{
240		ftk_widget_set_text(item, _("Stop VNC"));
241	}
242	else
243	{
244		ftk_widget_set_text(item, _("Start VNC"));
245	}
246	ftk_menu_item_set_clicked_listener(item, desktop_on_vnc, ctx);
247	ftk_widget_show(item, 1);
248#endif
249
250	return	RET_OK;
251}
252
253static Ret desktop_add_time_item_on_statusbar(void)
254{
255	FtkWidget* item = NULL;
256	FtkWidget* panel = NULL;
257
258	panel = ftk_default_status_panel();
259	if(panel != NULL)
260	{
261		item = ftk_status_item_create(panel, -2, 60);
262		ftk_widget_set_id(item, IDC_TIME_ITEM);
263		ftk_widget_show(item, 1);
264	}
265
266	ftk_logd("%s\n", __func__);
267	return RET_OK;
268}
269
270static void desktop_destroy(void* data)
271{
272	ftk_icon_cache_destroy(g_desktop.icon_cache);
273	app_info_manager_destroy(g_desktop.app_manager);
274
275	return;
276}
277
278int FTK_MAIN(int argc, char* argv[])
279{
280	int i = 0;
281	FtkWidget* win = NULL;
282	FtkWidget* button = NULL;
283	FtkSource* timer = NULL;
284	char path[FTK_MAX_PATH] = {0};
285	const char* root_path[FTK_ICON_PATH_NR] = {0};
286
287	ftk_init(argc, argv);
288
289	for(i = 0; i < argc; i++)
290	{
291		const char* key_play="--event-play=";
292		const char* key_record="--event-record=";
293#ifdef FTK_HAS_TESTER
294		if(strncmp(argv[i], key_play, strlen(key_play)) == 0)
295		{
296			ftk_tester_start_play(argv[i] + strlen(key_play));	
297		}
298		
299		if(strncmp(argv[i], key_record, strlen(key_record)) == 0)
300		{
301			ftk_tester_start_record(argv[i] + strlen(key_record));	
302		}
303#endif
304	}
305
306#ifdef ENABLE_NLS
307	if(getenv("LANG") == NULL)
308	{
309		setenv("LANG", "zh_CN.UTF-8", 1);
310		setlocale (LC_ALL, "zh_CN.UTF-8");
311		ftk_logd("LANG is not set, use zh_CN.UTF-8\n");
312	}
313	else
314	{
315		setlocale (LC_ALL, "");
316	}
317
318	bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
319	textdomain (PACKAGE); 
320	ftk_logd("%s: locale=%s\n", _("Hello, GetText"), setlocale(LC_ALL, NULL));
321#endif
322
323	desktop_add_time_item_on_statusbar();
324
325	if(argv[1] != NULL && strncmp(argv[1], "--hor", 5) == 0)
326	{
327		g_desktop.is_horizonal = 1;
328	}
329
330	g_desktop.app_manager = app_info_manager_create();
331
332	ftk_snprintf(path, sizeof(path), "%s/base/apps", FTK_ROOT_DIR);
333	ftk_normalize_path(path);
334	if(app_info_manager_load_dir(g_desktop.app_manager, path) != RET_OK)
335	{
336		ftk_snprintf(path, sizeof(path), "%s/apps", LOCAL_DATA_DIR);
337		app_info_manager_load_dir(g_desktop.app_manager, path);
338	}
339
340	ftk_snprintf(path, sizeof(path), "%s/desktop", FTK_ROOT_DIR);
341	root_path[0] = path;
342	root_path[1] = NULL;
343
344	g_desktop.icon_cache = ftk_icon_cache_create(root_path, NULL);
345	win = desktop_load_xul(g_desktop.is_horizonal ? "xul/desktop-h.xul" : "xul/desktop-v.xul"); 
346	ftk_app_window_set_on_prepare_options_menu(win, desktop_on_prepare_options_menu, win);
347	button = ftk_widget_lookup(win, 100);
348	ftk_button_set_clicked_listener(button, desktop_on_button_open_applist_clicked, win);
349	ftk_widget_show_all(win, 1);
350
351	desktop_update_time(win);
352	timer = ftk_source_timer_create(60000, desktop_update_time, win);
353	ftk_main_loop_add_source(ftk_default_main_loop(), timer);
354	ftk_widget_set_user_data(win, desktop_destroy, &g_desktop);
355
356	ftk_run();
357
358	return 0;
359}