OSDN Git Service

LinGui: add option to completely disable update checking
[handbrake-jp/handbrake-jp-git.git] / gtk / src / main.c
index aade07e..6d0d814 100644 (file)
 #if !defined(_WIN32)
 #include <gst/gst.h>
 #include <libnotify/notify.h>
+#include <dbus/dbus-glib.h>
 #else
 #include <windows.h>
 #include <io.h>
-//#include <pthread/pthread.h>
 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
 #endif
 
@@ -519,11 +519,121 @@ bind_presets_tree_model (signal_user_data_t *ud)
 }
 
 static void
+clean_old_logs()
+{
+#if !defined(_WIN32)
+       const gchar *file;
+       gchar *config;
+
+       config = ghb_get_user_config_dir(NULL);
+
+       if (g_file_test(config, G_FILE_TEST_IS_DIR))
+       {
+               GDir *gdir = g_dir_open(config, 0, NULL);
+               file = g_dir_read_name(gdir);
+               while (file)
+               {
+                       if (strncmp(file, "Activity.log.", 13) == 0)
+                       {
+                               gchar *path;
+                               int fd, lock = 1;
+                               int pid;
+
+                               sscanf(file, "Activity.log.%d", &pid);
+
+                               path = g_strdup_printf("%s/ghb.pid.%d", config, pid);
+                               if (g_file_test(path, G_FILE_TEST_EXISTS))
+                               {
+                                       fd = open(path, O_RDWR);
+                                       if (fd >= 0)
+                                       {
+                                               lock = lockf(fd, F_TLOCK, 0);
+                                       }
+                                       g_free(path);
+                                       close(fd);
+                                       if (lock == 0)
+                                       {
+                                               path = g_strdup_printf("%s/%s", config, file);
+                                               g_unlink(path);
+                                               g_free(path);
+                                       }
+                               }
+                               else
+                               {
+                                       g_free(path);
+                                       path = g_strdup_printf("%s/%s", config, file);
+                                       g_unlink(path);
+                                       g_free(path);
+                               }
+                       }
+                       file = g_dir_read_name(gdir);
+               }
+               g_dir_close(gdir);
+       }
+       g_free(config);
+#else
+       const gchar *file;
+       gchar *config;
+
+       config = ghb_get_user_config_dir(NULL);
+
+       if (g_file_test(config, G_FILE_TEST_IS_DIR))
+       {
+               GDir *gdir = g_dir_open(config, 0, NULL);
+               file = g_dir_read_name(gdir);
+               while (file)
+               {
+                       if (strncmp(file, "Activity.log.", 13) == 0)
+                       {
+                               gchar *path;
+                               int pid;
+
+                               sscanf(file, "Activity.log.%d", &pid);
+
+#if 0
+                               int fd, lock = 1;
+
+                               path = g_strdup_printf("%s/ghb.pid.%d", config, pid);
+                               if (g_file_test(path, G_FILE_TEST_EXISTS))
+                               {
+                                       fd = open(path, O_RDWR);
+                                       if (fd >= 0)
+                                       {
+                                               lock = lockf(fd, F_TLOCK, 0);
+                                       }
+                                       g_free(path);
+                                       close(fd);
+                                       if (lock == 0)
+                                       {
+                                               path = g_strdup_printf("%s/%s", config, file);
+                                               g_unlink(path);
+                                               g_free(path);
+                                       }
+                               }
+                               else
+#endif
+                               {
+                                       //g_free(path);
+                                       path = g_strdup_printf("%s/%s", config, file);
+                                       g_unlink(path);
+                                       g_free(path);
+                               }
+                       }
+                       file = g_dir_read_name(gdir);
+               }
+               g_dir_close(gdir);
+       }
+       g_free(config);
+#endif
+}
+
+static void
 IoRedirect(signal_user_data_t *ud)
 {
        GIOChannel *channel;
        gint pfd[2];
        gchar *config, *path, *str;
+       pid_t pid;
 
        // I'm opening a pipe and attaching the writer end to stderr
        // The reader end will be polled by main event loop and I'll get
@@ -533,10 +643,11 @@ IoRedirect(signal_user_data_t *ud)
                g_warning("Failed to redirect IO. Logging impaired\n");
                return;
        }
+       clean_old_logs();
        // Open activity log.
-       // TODO: Put this in the same directory as the encode destination
        config = ghb_get_user_config_dir(NULL);
-       path = g_strdup_printf("%s/%s", config, "Activity.log");
+       pid = getpid();
+       path = g_strdup_printf("%s/Activity.log.%d", config, pid);
        ud->activity_log = g_io_channel_new_file (path, "w", NULL);
        ud->job_activity_log = NULL;
        str = g_strdup_printf("<big><b>%s</b></big>", path);
@@ -547,7 +658,12 @@ IoRedirect(signal_user_data_t *ud)
        // Set encoding to raw.
        g_io_channel_set_encoding (ud->activity_log, NULL, NULL);
        // redirect stderr to the writer end of the pipe
+#if defined(_WIN32)
+       // dup2 doesn't work on windows for some stupid reason
+       stderr->_file = pfd[1];
+#else
        dup2(pfd[1], /*stderr*/2);
+#endif
        setvbuf(stderr, NULL, _IONBF, 0);
        channel = g_io_channel_unix_new (pfd[0]);
        // I was getting an this error:
@@ -666,11 +782,6 @@ main (int argc, char *argv[])
        textdomain (GETTEXT_PACKAGE);
 #endif
 
-#ifdef PTW32_STATIC_LIB
-       pthread_win32_process_attach_np();
-       pthread_win32_thread_attach_np();
-#endif
-
        if (!g_thread_supported())
                g_thread_init(NULL);
        context = g_option_context_new ("- Rip and encode DVD or MPEG file");
@@ -695,8 +806,9 @@ main (int argc, char *argv[])
        ghb_load_icons();
 
 #if !defined(_WIN32)
-       ghb_hal_init();
+       dbus_g_thread_init();
 #endif
+       ghb_udev_init();
 
        ghb_write_pid_file();
        ud = g_malloc0(sizeof(signal_user_data_t));
@@ -715,6 +827,11 @@ main (int argc, char *argv[])
        // Since GtkBuilder no longer assigns object ids to widget names
        // Assign a few that are necessary for style overrides to work
        GtkWidget *widget;
+#if defined(_NO_UPDATE_CHECK)
+       widget = GHB_WIDGET(ud->builder, "check_updates_box");
+       gtk_widget_hide(widget);
+#endif
+
        widget = GHB_WIDGET(ud->builder, "preview_hud");
        gtk_widget_set_name(widget, "preview_hud");
        widget = GHB_WIDGET(ud->builder, "preview_window");
@@ -865,9 +982,6 @@ main (int argc, char *argv[])
        notify_uninit();
 #endif
        g_free(ud);
-#ifdef PTW32_STATIC_LIB
-       pthread_win32_thread_detach_np();
-       pthread_win32_process_detach_np();
-#endif
+
        return 0;
 }