summaryrefslogtreecommitdiff
path: root/tumblerd/main.c
blob: e4d066699a23f22c7a1be546b15c5d062e5281d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * Copyright (c) 2009-2012 Jannis Pohlmann <jannis@xfce.org>
 * Copyright (c) 2015      Ali Abdallah    <ali@xfce.org>
 *  
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <stdlib.h>

#include <glib.h>
#include <glib-object.h>

#include <libxfce4util/libxfce4util.h>

#include <tumbler/tumbler.h>

#include <tumblerd/tumbler-cache-service.h>
#include <tumblerd/tumbler-lifecycle-manager.h>
#include <tumblerd/tumbler-manager.h>
#include <tumblerd/tumbler-registry.h>
#include <tumblerd/tumbler-service.h>



static void
shutdown_tumbler (TumblerLifecycleManager *lifecycle_manager,
                  GMainLoop               *main_loop)
{
  g_return_if_fail (TUMBLER_IS_LIFECYCLE_MANAGER (lifecycle_manager));
  g_return_if_fail (main_loop != NULL);

  /* exit the main loop */
  g_main_loop_quit (main_loop);
}



static void
on_dbus_name_lost (GDBusConnection *connection,
                   const gchar     *name,
                   gpointer         user_data)
{
  GMainLoop *main_loop;

  g_critical ("Name %s lost on the message dbus, exiting.",name);
  main_loop = (GMainLoop*)user_data;
  g_main_loop_quit(main_loop);
}

int
main (int    argc,
      char **argv)
{
  TumblerLifecycleManager *lifecycle_manager;
  TumblerProviderFactory  *provider_factory;
  GDBusConnection         *connection;
  TumblerRegistry         *registry;
  TumblerManager          *manager;
  TumblerService          *service;
  TumblerCacheService     *cache_service;
  GMainLoop               *main_loop;
  GError                  *error = NULL;
  GList                   *providers;
  GList                   *thumbnailers;
  GList                   *lp;
  GList                   *tp;
  gint                     retval = EXIT_SUCCESS;
  GKeyFile                *rc;
  gint64                   file_size;
  gint                     priority;
  const gchar             *type_name;
  gchar                  **paths;
  GSList                  *locations;
  GSList                  *excludes;

  /* set the program name */
  g_set_prgname (G_LOG_DOMAIN);

#ifdef G_OS_UNIX
  if (nice (19) != 19)
    g_warning ("Couldn't change nice value of process.");
#endif

#ifdef DEBUG
  /* if something doesn't work, fix your code instead! */
  g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
#endif

#if GLIB_CHECK_VERSION (2, 68, 0)
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  /* to avoid overlap between stderr and stdout, e.g. when third party APIs write to
   * stderr, or if debugging macros writing to stderr are used in addition to g_debug() */
  g_log_writer_default_set_use_stderr (TRUE);
  G_GNUC_END_IGNORE_DEPRECATIONS
#endif

  /* initialize translations */
  xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");

  /* set the application name. Translators: Don't translate "Tumbler". */
  g_set_application_name ("Tumbler Thumbnailing Service");

  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
  
  if (error != NULL)
    {
      g_critical ("error getting session bus: %s", error->message);
      g_error_free (error);
      
      return  EXIT_FAILURE;
    }
  
  /* create the lifecycle manager */
  lifecycle_manager = tumbler_lifecycle_manager_new ();

  /* create the thumbnailer registry */
  registry = tumbler_registry_new ();

  /* take a reference on the provider factory */
  provider_factory = tumbler_provider_factory_get_default ();

  /* query all thumbnailer providers from the factory */
  providers = tumbler_provider_factory_get_providers (provider_factory,
                                                      TUMBLER_TYPE_THUMBNAILER_PROVIDER);

  /* settings */
  rc = tumbler_util_get_settings ();

  /* iterate over all providers */
  for (lp = providers; lp != NULL; lp = lp->next)
    {
      /* query the list of thumbnailers provided by this provider */
      thumbnailers = tumbler_thumbnailer_provider_get_thumbnailers (lp->data);

      /* add all thumbnailers to the registry */
      for (tp = thumbnailers; tp != NULL; tp = tp->next)
        {
          /* desktop thumbnailers are set up per desktop file */
          if (g_object_class_find_property (G_OBJECT_GET_CLASS (tp->data), "exec") == NULL)
            {
              /* set settings from rc file */
              type_name = G_OBJECT_TYPE_NAME (tp->data);
              priority = g_key_file_get_integer (rc, type_name, "Priority", NULL);
              file_size = g_key_file_get_int64 (rc, type_name, "MaxFileSize", NULL);

              paths = g_key_file_get_string_list (rc, type_name, "Locations", NULL, NULL);
              locations = tumbler_util_locations_from_strv (paths);
              g_strfreev (paths);

              paths = g_key_file_get_string_list (rc, type_name, "Excludes", NULL, NULL);
              excludes = tumbler_util_locations_from_strv (paths);
              g_strfreev (paths);

              g_object_set (tp->data, "priority", priority, "max-file-size", file_size,
                            "locations", locations, "excludes", excludes, NULL);

              /* cleanup */
              g_slist_free_full (locations, g_object_unref);
              g_slist_free_full (excludes, g_object_unref);
            }

          /* ready for usage */
          tumbler_registry_add (registry, tp->data);
        }

      /* free the thumbnailer list */
      g_list_free_full (thumbnailers, g_object_unref);
    }

  g_key_file_free (rc);

  /* release all providers and free the provider list */
  g_list_free_full (providers, g_object_unref);

  /* drop the reference on the provider factory */
  g_object_unref (provider_factory);

  /* update the URI schemes / MIME types supported information */
  tumbler_registry_update_supported (registry);

  /* create the thumbnail cache service */
  cache_service = tumbler_cache_service_new (connection, lifecycle_manager);

  /* create the thumbnailer manager service */
  manager = tumbler_manager_new (connection, lifecycle_manager, registry);

  /* create the generic thumbnailer service */
  service = tumbler_service_new (connection, lifecycle_manager, registry);

  /* try to load specialized thumbnailers and exit if that fails */
  if (!tumbler_registry_load (registry, &error))
    {
      g_warning ("Failed to load specialized thumbnailers into the registry: %s",
                 error->message);
      g_error_free (error);

      /* something failed */
      retval = EXIT_FAILURE;
      goto exit_tumbler;
    }

  /* create a new main loop */
  main_loop = g_main_loop_new (NULL, FALSE);
      
  
  /* Acquire the cache service dbus name */
  g_bus_own_name_on_connection (connection,
                                TUMBLER_SERVICE_NAME_PREFIX ".Cache1",
                                G_BUS_NAME_OWNER_FLAGS_REPLACE,
                                NULL, /* We dont need to do anything on name acquired*/
                                on_dbus_name_lost,
                                main_loop,
                                NULL);

  /* Acquire the manager dbus name */
  g_bus_own_name_on_connection (connection,
                                TUMBLER_SERVICE_NAME_PREFIX ".Manager1",
                                G_BUS_NAME_OWNER_FLAGS_REPLACE,
                                NULL, /* We dont need to do anything on name acquired*/
                                on_dbus_name_lost,
                                main_loop,
                                NULL);

  /* Acquire the thumbnailer service dbus name */
  g_bus_own_name_on_connection (connection,
                                TUMBLER_SERVICE_NAME_PREFIX ".Thumbnailer1",
                                G_BUS_NAME_OWNER_FLAGS_REPLACE,
                                NULL, /* We dont need to do anything on name acquired*/
                                on_dbus_name_lost,
                                main_loop,
                                NULL);
  
  /* check to see if all services are successfully exported on the bus */
  if (tumbler_manager_is_exported(manager) &&
      tumbler_service_is_exported(service) &&
      tumbler_cache_service_is_exported(cache_service))
    {
      /* Let the manager initializes the thumbnailer
       * directory objects, directory monitors */
      tumbler_manager_load (manager);
        
      /* quit the main loop when the lifecycle manager asks us to shut down */
      g_signal_connect (lifecycle_manager, "shutdown", 
                        G_CALLBACK (shutdown_tumbler), main_loop);

      /* start the lifecycle manager */
      tumbler_lifecycle_manager_start (lifecycle_manager);

      g_debug ("Ready to handle requests\n");

      /* enter the main loop, thereby making the tumbler service available */
      g_main_loop_run (main_loop);
    }
  
  exit_tumbler:

  /* shut our services down and release all objects */
  g_object_unref (service);
  g_object_unref (manager);
  g_object_unref (cache_service);
  g_object_unref (registry);
  g_object_unref (lifecycle_manager);

  /* free the dbus session bus connection */
  g_object_unref (connection);

  /* we're done, all fine */
  return retval;
}