nemo-desktop-application.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
  2. /*
  3. * nemo-application: main Nemo application class.
  4. *
  5. * Copyright (C) 1999, 2000 Red Hat, Inc.
  6. * Copyright (C) 2000, 2001 Eazel, Inc.
  7. * Copyright (C) 2010, Cosimo Cecchi <cosimoc@gnome.org>
  8. *
  9. * Nemo is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * Nemo is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Suite 500, MA 02110-1335, USA.
  22. *
  23. * Authors: Elliot Lee <sopwith@redhat.com>,
  24. * Darin Adler <darin@bentspoon.com>
  25. * Cosimo Cecchi <cosimoc@gnome.org>
  26. *
  27. */
  28. #include <config.h>
  29. #include "nemo-desktop-application.h"
  30. #include "nemo-desktop-icon-view.h"
  31. #include "nemo-desktop-icon-grid-view.h"
  32. #include "nemo-icon-view.h"
  33. #include "nemo-list-view.h"
  34. #include "nemo-freedesktop-dbus.h"
  35. #include "nemo-desktop-manager.h"
  36. #include "nemo-image-properties-page.h"
  37. #include "nemo-previewer.h"
  38. #include "nemo-progress-ui-handler.h"
  39. #include "nemo-window-bookmarks.h"
  40. #include "nemo-window-private.h"
  41. #include <eel/eel-vfs-extensions.h>
  42. #include <libnemo-private/nemo-desktop-link-monitor.h>
  43. #include <libnemo-private/nemo-desktop-metadata.h>
  44. #include <libnemo-private/nemo-file-utilities.h>
  45. #include <libnemo-private/nemo-global-preferences.h>
  46. #include <libnemo-private/nemo-module.h>
  47. #include <libnemo-private/nemo-signaller.h>
  48. #include <libnemo-private/nemo-undo-manager.h>
  49. #include <libnemo-extension/nemo-menu-provider.h>
  50. #define DEBUG_FLAG NEMO_DEBUG_APPLICATION
  51. #include <libnemo-private/nemo-debug.h>
  52. #include <sys/types.h>
  53. #include <fcntl.h>
  54. #include <errno.h>
  55. #include <stdlib.h>
  56. #include <glib/gstdio.h>
  57. #include <glib/gi18n.h>
  58. #include <libnotify/notify.h>
  59. #include <gdk/gdkx.h>
  60. #include <X11/Xatom.h>
  61. G_DEFINE_TYPE (NemoDesktopApplication, nemo_desktop_application, NEMO_TYPE_APPLICATION);
  62. struct _NemoDesktopApplicationPriv {
  63. NemoDesktopManager *desktop_manager;
  64. NemoFreedesktopDBus *fdb_manager;
  65. };
  66. static void
  67. nemo_desktop_application_init (NemoDesktopApplication *application)
  68. {
  69. application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
  70. NEMO_TYPE_DESKTOP_APPLICATION,
  71. NemoDesktopApplicationPriv);
  72. }
  73. static void
  74. nemo_desktop_application_finalize (GObject *object)
  75. {
  76. NemoDesktopApplication *app = NEMO_DESKTOP_APPLICATION (object);
  77. g_clear_object (&app->priv->fdb_manager);
  78. G_OBJECT_CLASS (nemo_desktop_application_parent_class)->finalize (object);
  79. }
  80. static void
  81. init_desktop (NemoDesktopApplication *self)
  82. {
  83. /* Initialize the desktop link monitor singleton */
  84. nemo_desktop_link_monitor_get ();
  85. nemo_desktop_metadata_init ();
  86. self->priv->desktop_manager = nemo_desktop_manager_get ();
  87. }
  88. /* from libwnck/xutils.c, comes as LGPLv2+ */
  89. static char *
  90. latin1_to_utf8 (const char *latin1)
  91. {
  92. GString *str;
  93. const char *p;
  94. str = g_string_new (NULL);
  95. p = latin1;
  96. while (*p)
  97. {
  98. g_string_append_unichar (str, (gunichar) *p);
  99. ++p;
  100. }
  101. return g_string_free (str, FALSE);
  102. }
  103. /* derived from libwnck/xutils.c, comes as LGPLv2+ */
  104. static void
  105. _get_wmclass (Display *xdisplay,
  106. Window xwindow,
  107. char **res_class,
  108. char **res_name)
  109. {
  110. XClassHint ch;
  111. ch.res_name = NULL;
  112. ch.res_class = NULL;
  113. gdk_error_trap_push ();
  114. XGetClassHint (xdisplay, xwindow, &ch);
  115. gdk_error_trap_pop_ignored ();
  116. if (res_class)
  117. *res_class = NULL;
  118. if (res_name)
  119. *res_name = NULL;
  120. if (ch.res_name)
  121. {
  122. if (res_name)
  123. *res_name = latin1_to_utf8 (ch.res_name);
  124. XFree (ch.res_name);
  125. }
  126. if (ch.res_class)
  127. {
  128. if (res_class)
  129. *res_class = latin1_to_utf8 (ch.res_class);
  130. XFree (ch.res_class);
  131. }
  132. }
  133. static gboolean
  134. desktop_handler_is_ignored (GdkWindow *window, gchar **ignored)
  135. {
  136. gboolean ret;
  137. Window xw;
  138. Display *xd;
  139. guint i;
  140. if (ignored == NULL) {
  141. return FALSE;
  142. }
  143. ret = FALSE;
  144. xw = gdk_x11_window_get_xid (GDK_X11_WINDOW (window));
  145. xd = gdk_x11_display_get_xdisplay (gdk_display_get_default ());
  146. for (i = 0; i < g_strv_length (ignored); i++) {
  147. gchar *wmclass, *wm_class_casefolded, *match_string_casefolded;
  148. _get_wmclass (xd, xw, &wmclass, NULL);
  149. if (wmclass != NULL) {
  150. wm_class_casefolded = g_utf8_casefold (wmclass, -1);
  151. match_string_casefolded = g_utf8_casefold (ignored[i], -1);
  152. if (g_strstr_len (wm_class_casefolded, -1, match_string_casefolded) != NULL) {
  153. ret = TRUE;
  154. }
  155. g_free (wm_class_casefolded);
  156. g_free (match_string_casefolded);
  157. g_free (wmclass);
  158. }
  159. if (ret == TRUE)
  160. break;
  161. }
  162. return ret;
  163. }
  164. static gboolean
  165. desktop_already_managed (void)
  166. {
  167. GdkScreen *screen;
  168. GList *windows, *iter;
  169. gboolean ret;
  170. screen = gdk_screen_get_default ();
  171. windows = gdk_screen_get_window_stack (screen);
  172. ret = FALSE;
  173. for (iter = windows; iter != NULL; iter = iter->next) {
  174. GdkWindow *window = GDK_WINDOW (iter->data);
  175. if (gdk_window_get_type_hint (window) == GDK_WINDOW_TYPE_HINT_DESKTOP) {
  176. GSettings *desktop_preferences = g_settings_new("org.nemo.desktop");
  177. gchar **ignored = g_settings_get_strv (desktop_preferences, NEMO_PREFERENCES_DESKTOP_IGNORED_DESKTOP_HANDLERS);
  178. if (!desktop_handler_is_ignored (window, ignored)) {
  179. ret = TRUE;
  180. }
  181. g_strfreev (ignored);
  182. g_object_unref (desktop_preferences);
  183. break;
  184. }
  185. }
  186. g_list_free_full (windows, (GDestroyNotify) g_object_unref);
  187. if (ret) {
  188. g_warning ("Desktop already managed by another application, skipping desktop setup.\n"
  189. "To change this, modify org.nemo.desktop 'ignored-desktop-handlers'.\n");
  190. }
  191. return ret;
  192. }
  193. static gboolean
  194. nemo_desktop_application_local_command_line (GApplication *application,
  195. gchar ***arguments,
  196. gint *exit_status)
  197. {
  198. GFile **files;
  199. gint len;
  200. gboolean version = FALSE;
  201. gboolean kill_shell = FALSE;
  202. gboolean debug = FALSE;
  203. const GOptionEntry options[] = {
  204. { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
  205. N_("Show the version of the program."), NULL },
  206. { "debug", 0, 0, G_OPTION_ARG_NONE, &debug,
  207. "Enable debugging code. Example usage: 'NEMO_DEBUG=Desktop,Actions nemo-desktop --debug'. Use NEMO_DEBUG=all for more topics.", NULL },
  208. { "quit", 'q', 0, G_OPTION_ARG_NONE, &kill_shell,
  209. N_("Quit Nemo Desktop."), NULL },
  210. { NULL }
  211. };
  212. GOptionContext *context;
  213. GError *error = NULL;
  214. gint argc = 0;
  215. gchar **argv = NULL;
  216. *exit_status = EXIT_SUCCESS;
  217. context = g_option_context_new (_("\n\nManage the desktop with the file manager"));
  218. g_option_context_add_main_entries (context, options, NULL);
  219. g_option_context_add_group (context, gtk_get_option_group (TRUE));
  220. argv = *arguments;
  221. argc = g_strv_length (argv);
  222. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  223. g_printerr ("Could not parse arguments: %s\n", error->message);
  224. g_error_free (error);
  225. *exit_status = EXIT_FAILURE;
  226. goto out;
  227. }
  228. if (version) {
  229. g_print ("nemo-desktop " VERSION "\n");
  230. goto out;
  231. }
  232. if (debug) {
  233. g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
  234. }
  235. g_application_register (application, NULL, &error);
  236. if (error != NULL) {
  237. g_printerr ("Could not register the application: %s\n", error->message);
  238. g_error_free (error);
  239. *exit_status = EXIT_FAILURE;
  240. goto out;
  241. }
  242. if (kill_shell) {
  243. g_printerr ("Killing nemo-desktop, as requested\n");
  244. g_action_group_activate_action (G_ACTION_GROUP (application),
  245. "quit", NULL);
  246. goto out;
  247. }
  248. if (desktop_already_managed ()) {
  249. goto out;
  250. }
  251. files = g_malloc0 (2 * sizeof (GFile *));
  252. len = 1;
  253. files[0] = g_file_new_for_uri (EEL_DESKTOP_URI);
  254. files[1] = NULL;
  255. g_application_open (application, files, len, "");
  256. g_object_unref (files[0]);
  257. g_free (files);
  258. out:
  259. g_option_context_free (context);
  260. return TRUE;
  261. }
  262. static void
  263. nemo_desktop_application_continue_startup (NemoApplication *app)
  264. {
  265. /* Check the user's Desktop and config directories and post warnings
  266. * if there are problems.
  267. */
  268. nemo_application_check_required_directory (app, nemo_get_desktop_directory ());
  269. nemo_application_check_required_directory (app, nemo_get_user_directory ());
  270. NEMO_DESKTOP_APPLICATION (app)->priv->fdb_manager = nemo_freedesktop_dbus_new ();
  271. /* register views */
  272. nemo_desktop_icon_view_register ();
  273. nemo_desktop_icon_grid_view_register ();
  274. nemo_icon_view_register ();
  275. }
  276. static void
  277. nemo_desktop_application_continue_quit (NemoApplication *app)
  278. {
  279. NemoDesktopApplication *self = NEMO_DESKTOP_APPLICATION (app);
  280. g_clear_object (&self->priv->desktop_manager);
  281. }
  282. static void
  283. nemo_desktop_application_open (GApplication *app,
  284. GFile **files,
  285. gint n_files,
  286. const gchar *geometry)
  287. {
  288. NemoDesktopApplication *self = NEMO_DESKTOP_APPLICATION (app);
  289. DEBUG ("Open called on the GApplication instance; %d files", n_files);
  290. if (self->priv->desktop_manager == NULL) {
  291. init_desktop (self);
  292. }
  293. /* FIXME: how to do this? */
  294. }
  295. static void
  296. nemo_desktop_application_open_location (NemoApplication *application,
  297. GFile *location,
  298. GFile *selection,
  299. const char *startup_id,
  300. const gboolean open_in_tabs)
  301. {
  302. GAppInfo *appinfo;
  303. GError *error = NULL;
  304. GList *sel_list = NULL;
  305. appinfo = g_app_info_get_default_for_type ("inode/directory", TRUE);
  306. if (!appinfo) {
  307. g_warning ("Cannot launch file browser, no mimetype handler for inode/directory");
  308. return;
  309. }
  310. if (selection != NULL) {
  311. sel_list = g_list_prepend (sel_list, selection);
  312. } else if (location != NULL) {
  313. sel_list = g_list_prepend (sel_list, location);
  314. }
  315. if (!g_app_info_launch (appinfo, sel_list, NULL, &error)) {
  316. gchar *uri;
  317. uri = g_file_get_uri (selection);
  318. g_warning ("Could not launch file browser to display file: %s\n", uri);
  319. g_free (uri);
  320. g_clear_error (&error);
  321. }
  322. if (sel_list != NULL) {
  323. g_list_free (sel_list);
  324. }
  325. g_clear_object (&appinfo);
  326. }
  327. static NemoWindow *
  328. nemo_desktop_application_create_window (NemoApplication *application,
  329. GdkScreen *screen)
  330. {
  331. return NULL;
  332. }
  333. static void
  334. nemo_desktop_application_class_init (NemoDesktopApplicationClass *class)
  335. {
  336. GObjectClass *object_class;
  337. GApplicationClass *application_class;
  338. NemoApplicationClass *nemo_app_class;
  339. object_class = G_OBJECT_CLASS (class);
  340. object_class->finalize = nemo_desktop_application_finalize;
  341. application_class = G_APPLICATION_CLASS (class);
  342. application_class->open = nemo_desktop_application_open;
  343. application_class->local_command_line = nemo_desktop_application_local_command_line;
  344. nemo_app_class = NEMO_APPLICATION_CLASS (class);
  345. nemo_app_class->continue_startup = nemo_desktop_application_continue_startup;
  346. nemo_app_class->create_window = nemo_desktop_application_create_window;
  347. nemo_app_class->continue_quit = nemo_desktop_application_continue_quit;
  348. nemo_app_class->open_location = nemo_desktop_application_open_location;
  349. g_type_class_add_private (class, sizeof (NemoDesktopApplicationPriv));
  350. }
  351. NemoApplication *
  352. nemo_desktop_application_get_singleton (void)
  353. {
  354. return nemo_application_initialize_singleton (NEMO_TYPE_DESKTOP_APPLICATION,
  355. "application-id", "org.NemoDesktop",
  356. "flags", G_APPLICATION_HANDLES_OPEN,
  357. "register-session", TRUE,
  358. NULL);
  359. }