/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. * History:
  26. * ================================================================
  27. * 2009-11-29 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk.h"
  31. #include <time.h>
  32. #include "ftk_xul.h"
  33. #include "ftk_util.h"
  34. #include "app_info.h"
  35. #include "ftk_tester.h"
  36. #include "vnc_service.h"
  37. typedef struct _FtkDesktop
  38. {
  39. int is_horizonal;
  40. FtkWidget* applist_win;
  41. AppInfoManager* app_manager;
  42. FtkIconCache* icon_cache;
  43. }FtkDesktop;
  44. static FtkDesktop g_desktop;
  45. static Ret desktop_on_button_close_applist_clicked(void* ctx, void* obj)
  46. {
  47. ftk_widget_show(ctx, 0);
  48. return RET_OK;
  49. }
  50. static const char* desktop_translate_text(void* ctx, const char* text)
  51. {
  52. return _(text);
  53. }
  54. static const char* desktop_translate_path(const char* path, char out_path[FTK_MAX_PATH+1])
  55. {
  56. ftk_snprintf(out_path, FTK_MAX_PATH, "%s/desktop/%s", FTK_ROOT_DIR, path);
  57. ftk_normalize_path(out_path);
  58. ftk_logd("%s: %s --> %s\n", __func__, path, out_path);
  59. return out_path;
  60. }
  61. static FtkWidget* desktop_load_xul(const char* filename)
  62. {
  63. FtkXulCallbacks callbacks = {0};
  64. char path[FTK_MAX_PATH+1] = {0};
  65. callbacks.ctx = g_desktop.icon_cache;
  66. callbacks.translate_text = desktop_translate_text;
  67. callbacks.load_image = (FtkXulLoadImage)ftk_icon_cache_load;
  68. desktop_translate_path(filename, path);
  69. return ftk_xul_load_file(path, &callbacks);
  70. }
  71. static Ret applist_on_item_clicked(void* ctx, void* obj)
  72. {
  73. FtkIconViewItem* item = obj;
  74. AppInfo* info = item->user_data;
  75. ftk_app_run(info->app, 0, NULL);
  76. ftk_logd("%s: %s: user_data=%d\n", __func__, item->text, (int)item->user_data);
  77. return RET_OK;
  78. }
  79. static Ret desktop_on_button_open_applist_clicked(void* ctx, void* obj)
  80. {
  81. size_t i = 0;
  82. size_t n = 0;
  83. size_t item_size = 100;
  84. FtkWidget* win = NULL;
  85. AppInfo* app_info = NULL;
  86. FtkWidget* button = NULL;
  87. FtkIconViewItem item;
  88. FtkWidget* icon_view = NULL;
  89. if(g_desktop.applist_win != NULL)
  90. {
  91. ftk_widget_show_all(g_desktop.applist_win, 1);
  92. return RET_OK;
  93. }
  94. win = desktop_load_xul(g_desktop.is_horizonal ? "xul/appview-h.xul" : "xul/appview-v.xul");
  95. button = ftk_widget_lookup(win, 100);
  96. ftk_button_set_clicked_listener(button, desktop_on_button_close_applist_clicked, win);
  97. icon_view = ftk_widget_lookup(win, 99);
  98. item_size = 6 * ftk_widget_get_font_size(icon_view);
  99. if(ftk_widget_width(icon_view) < 2 * item_size)
  100. {
  101. item_size = (ftk_widget_width(icon_view) - 10) / 2;
  102. }
  103. ftk_icon_view_set_item_size(icon_view, item_size);
  104. ftk_icon_view_set_clicked_listener(icon_view, applist_on_item_clicked, win);
  105. n = app_info_manager_get_count(g_desktop.app_manager);
  106. for(i = 0; i < n; i++)
  107. {
  108. app_info_manager_get(g_desktop.app_manager, i, &app_info);
  109. item.icon = ftk_app_get_icon(app_info->app);
  110. item.user_data = app_info;
  111. item.text = (char*)ftk_app_get_name(app_info->app);
  112. if(item.text == NULL)
  113. {
  114. item.text = app_info->name;
  115. }
  116. ftk_icon_view_add(icon_view, &item);
  117. }
  118. g_desktop.applist_win = win;
  119. return RET_OK;
  120. }
  121. #define IDC_TIME_ITEM 2000
  122. static Ret desktop_update_time(void* ctx)
  123. {
  124. char text[10] = {0};
  125. time_t now = time(0);
  126. FtkWidget* item = NULL;
  127. FtkWidget* panel = NULL;
  128. FtkWidget* win = ctx;
  129. FtkWidget* image = NULL;
  130. FtkBitmap* bitmap = NULL;
  131. char filename[FTK_MAX_PATH] = {0};
  132. struct tm* t = localtime(&now);
  133. panel = ftk_default_status_panel();
  134. if(panel == NULL)
  135. {
  136. return RET_REMOVE;
  137. }
  138. ftk_snprintf(text, sizeof(text)-1, "%d:%02d", t->tm_hour, t->tm_min);
  139. item = ftk_widget_lookup(panel, IDC_TIME_ITEM);
  140. ftk_widget_set_text(item, text);
  141. ftk_logd("%s\n", __func__);
  142. image = ftk_widget_lookup(win, 1);
  143. ftk_snprintf(filename, sizeof(filename)-1, "icons/%d.png", t->tm_hour/10);
  144. bitmap = ftk_icon_cache_load(g_desktop.icon_cache, filename);
  145. ftk_image_set_image(image, bitmap);
  146. image = ftk_widget_lookup(win, 2);
  147. ftk_snprintf(filename, sizeof(filename)-1, "icons/%d.png", t->tm_hour%10);
  148. bitmap = ftk_icon_cache_load(g_desktop.icon_cache, filename);
  149. ftk_image_set_image(image, bitmap);
  150. image = ftk_widget_lookup(win, 4);
  151. ftk_snprintf(filename, sizeof(filename)-1, "icons/%d.png", t->tm_min/10);
  152. bitmap = ftk_icon_cache_load(g_desktop.icon_cache, filename);
  153. ftk_image_set_image(image, bitmap);
  154. image = ftk_widget_lookup(win, 5);
  155. ftk_snprintf(filename, sizeof(filename)-1, "icons/%d.png", t->tm_min%10);
  156. bitmap = ftk_icon_cache_load(g_desktop.icon_cache, filename);
  157. ftk_image_set_image(image, bitmap);
  158. return RET_OK;
  159. }
  160. static Ret desktop_on_shutdown(void* ctx, void* obj)
  161. {
  162. #ifdef USE_VNC
  163. if(ftk_display_vnc_is_active())
  164. {
  165. ftk_display_vnc_quit();
  166. }
  167. #endif
  168. ftk_quit();
  169. return RET_OK;
  170. }
  171. #ifdef USE_VNC
  172. static Ret desktop_on_vnc(void* ctx, void* obj)
  173. {
  174. if(ftk_display_vnc_is_active())
  175. {
  176. ftk_display_vnc_stop();
  177. }
  178. else
  179. {
  180. ftk_display_vnc_start();
  181. }
  182. return RET_OK;
  183. }
  184. #endif
  185. static Ret desktop_on_prepare_options_menu(void* ctx, FtkWidget* menu_panel)
  186. {
  187. FtkWidget* item = ftk_menu_item_create(menu_panel);
  188. ftk_widget_set_text(item, _("Shutdown"));
  189. ftk_menu_item_set_clicked_listener(item, desktop_on_shutdown, ctx);
  190. ftk_widget_show(item, 1);
  191. #ifdef USE_VNC
  192. item = ftk_menu_item_create(menu_panel);
  193. if(ftk_display_vnc_is_active())
  194. {
  195. ftk_widget_set_text(item, _("Stop VNC"));
  196. }
  197. else
  198. {
  199. ftk_widget_set_text(item, _("Start VNC"));
  200. }
  201. ftk_menu_item_set_clicked_listener(item, desktop_on_vnc, ctx);
  202. ftk_widget_show(item, 1);
  203. #endif
  204. return RET_OK;
  205. }
  206. static Ret desktop_add_time_item_on_statusbar(void)
  207. {
  208. FtkWidget* item = NULL;
  209. FtkWidget* panel = NULL;
  210. panel = ftk_default_status_panel();
  211. if(panel != NULL)
  212. {
  213. item = ftk_status_item_create(panel, -2, 60);
  214. ftk_widget_set_id(item, IDC_TIME_ITEM);
  215. ftk_widget_show(item, 1);
  216. }
  217. ftk_logd("%s\n", __func__);
  218. return RET_OK;
  219. }
  220. static void desktop_destroy(void* data)
  221. {
  222. ftk_icon_cache_destroy(g_desktop.icon_cache);
  223. app_info_manager_destroy(g_desktop.app_manager);
  224. return;
  225. }
  226. int FTK_MAIN(int argc, char* argv[])
  227. {
  228. int i = 0;
  229. FtkWidget* win = NULL;
  230. FtkWidget* button = NULL;
  231. FtkSource* timer = NULL;
  232. char path[FTK_MAX_PATH] = {0};
  233. const char* root_path[FTK_ICON_PATH_NR] = {0};
  234. ftk_init(argc, argv);
  235. for(i = 0; i < argc; i++)
  236. {
  237. const char* key_play="--event-play=";
  238. const char* key_record="--event-record=";
  239. #ifdef FTK_HAS_TESTER
  240. if(strncmp(argv[i], key_play, strlen(key_play)) == 0)
  241. {
  242. ftk_tester_start_play(argv[i] + strlen(key_play));
  243. }
  244. if(strncmp(argv[i], key_record, strlen(key_record)) == 0)
  245. {
  246. ftk_tester_start_record(argv[i] + strlen(key_record));
  247. }
  248. #endif
  249. }
  250. #ifdef ENABLE_NLS
  251. if(getenv("LANG") == NULL)
  252. {
  253. setenv("LANG", "zh_CN.UTF-8", 1);
  254. setlocale (LC_ALL, "zh_CN.UTF-8");
  255. ftk_logd("LANG is not set, use zh_CN.UTF-8\n");
  256. }
  257. else
  258. {
  259. setlocale (LC_ALL, "");
  260. }
  261. bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
  262. textdomain (PACKAGE);
  263. ftk_logd("%s: locale=%s\n", _("Hello, GetText"), setlocale(LC_ALL, NULL));
  264. #endif
  265. desktop_add_time_item_on_statusbar();
  266. if(argv[1] != NULL && strncmp(argv[1], "--hor", 5) == 0)
  267. {
  268. g_desktop.is_horizonal = 1;
  269. }
  270. g_desktop.app_manager = app_info_manager_create();
  271. ftk_snprintf(path, sizeof(path), "%s/base/apps", FTK_ROOT_DIR);
  272. ftk_normalize_path(path);
  273. if(app_info_manager_load_dir(g_desktop.app_manager, path) != RET_OK)
  274. {
  275. ftk_snprintf(path, sizeof(path), "%s/apps", LOCAL_DATA_DIR);
  276. app_info_manager_load_dir(g_desktop.app_manager, path);
  277. }
  278. ftk_snprintf(path, sizeof(path), "%s/desktop", FTK_ROOT_DIR);
  279. root_path[0] = path;
  280. root_path[1] = NULL;
  281. g_desktop.icon_cache = ftk_icon_cache_create(root_path, NULL);
  282. win = desktop_load_xul(g_desktop.is_horizonal ? "xul/desktop-h.xul" : "xul/desktop-v.xul");
  283. ftk_app_window_set_on_prepare_options_menu(win, desktop_on_prepare_options_menu, win);
  284. button = ftk_widget_lookup(win, 100);
  285. ftk_button_set_clicked_listener(button, desktop_on_button_open_applist_clicked, win);
  286. ftk_widget_show_all(win, 1);
  287. desktop_update_time(win);
  288. timer = ftk_source_timer_create(60000, desktop_update_time, win);
  289. ftk_main_loop_add_source(ftk_default_main_loop(), timer);
  290. ftk_widget_set_user_data(win, desktop_destroy, &g_desktop);
  291. ftk_run();
  292. return 0;
  293. }