[bijiben/wip/sadiq/rewrite: 7/12] Add application class



commit 33154f892b4cee3241b27392c178f1269ef03fdd
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Tue Feb 27 07:48:12 2018 +0530

    Add application class

 src/bjb-app.c |  303 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bjb-app.h |   36 +++++++
 2 files changed, 339 insertions(+), 0 deletions(-)
---
diff --git a/src/bjb-app.c b/src/bjb-app.c
new file mode 100644
index 0000000..9f012c8
--- /dev/null
+++ b/src/bjb-app.c
@@ -0,0 +1,303 @@
+/* bjb-application.c
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "bjb-application"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+#include "bjb-window.h"
+#include "bjb-provider.h"
+#include "bjb-local-provider.h"
+
+#include "bjb-app.h"
+
+/**
+ * SECTION: bjb-application
+ * @title: bjb-application
+ * @short_description: Base Application class
+ * @include: "bjb-application.h"
+ *
+ */
+
+struct _BjbApplication
+{
+  GtkApplication  parent_instance;
+
+  GtkCssProvider *css_provider;
+
+  GHashTable *providers;
+};
+
+G_DEFINE_TYPE (BjbApplication, bjb_application, GTK_TYPE_APPLICATION)
+
+enum {
+  PROVIDER_ADDED,
+  N_SIGNALS
+};
+
+static guint signals[N_SIGNALS];
+
+static GSettings *settings;
+static GThread *main_thread;
+static GOptionEntry cmd_options[] = {
+  { "version", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, N_("Show release version") },
+  { NULL }
+};
+
+static void
+bjb_application_show_about (GSimpleAction *action,
+                            GVariant      *parameter,
+                            gpointer       user_data)
+{
+  GtkApplication *application = GTK_APPLICATION (user_data);
+  const gchar *authors[] = {
+    "Mohammed Sadiq <sadiq sadiqpk org>",
+    NULL
+  };
+
+  gtk_show_about_dialog (gtk_application_get_active_window (application),
+                         "program-name", _("GNOME Notes"),
+                         "website", "https://www.sadiqpk.org";,
+                         "version", "0.0.1",
+                         "copyright", "Copyright \xC2\xA9 2018 Mohammed Sadiq",
+                         "license-type", GTK_LICENSE_GPL_3_0,
+                         "authors", authors,
+                         "logo-icon-name", "org.gnome.bijiben",
+                         "translator-credits", _("translator-credits"),
+                         NULL);
+}
+
+static void
+bjb_application_show_help (GSimpleAction *action,
+                           GVariant      *parameter,
+                           gpointer       user_data)
+{
+  GtkWindow *window;
+  g_autoptr(GError) error = NULL;
+
+  window = gtk_application_get_active_window (GTK_APPLICATION (user_data));
+
+  if (!gtk_show_uri_on_window (window,
+                               "help:bijiben",
+                               GDK_CURRENT_TIME,
+                               &error))
+    g_warning ("Failed to launch help: %s", error->message);
+}
+
+static void
+bjb_application_quit (GSimpleAction *action,
+                      GVariant      *parameter,
+                      gpointer       user_data)
+{
+  g_application_quit (G_APPLICATION (user_data));
+}
+
+static const GActionEntry application_entries[] = {
+  { "about", bjb_application_show_about },
+  { "help",  bjb_application_show_help  },
+  { "quit",  bjb_application_quit       },
+};
+
+static void
+bjb_application_provider_ready_cb (BjbApplication *self,
+                                   BjbProvider    *provider)
+{
+  g_assert (BJB_IS_APPLICATION (self));
+  g_assert (BJB_IS_PROVIDER (provider));
+
+  g_signal_emit (self, signals[PROVIDER_ADDED], 0, provider);
+}
+
+static void
+bjb_application_add_provider (BjbApplication *self,
+                              BjbProvider    *provider)
+{
+  g_assert (BJB_IS_APPLICATION (self));
+  g_assert (BJB_IS_PROVIDER (provider));
+
+  g_hash_table_insert (self->providers,
+                       bjb_provider_get_uid (provider),
+                       provider);
+
+  g_signal_connect_object (provider, "ready",
+                           bjb_application_provider_ready_cb,
+                           self,
+                           G_CONNECT_SWAPPED);
+}
+
+static void
+bjb_application_load_providers (BjbApplication *self)
+{
+  BjbProvider *provider;
+  g_autoptr(GError) error;
+
+  g_assert (BJB_IS_APPLICATION (self));
+
+  provider = BJB_PROVIDER (bjb_local_provider_new (&error));
+
+  if (error)
+      g_message ("Could to enable Local provider: %s", error->message);
+  else
+    bjb_application_add_provider (self, provider);
+}
+
+static gint
+bjb_application_handle_local_options (GApplication *application,
+                                      GVariantDict *options)
+{
+  if (g_variant_dict_contains (options, "version"))
+    {
+      g_print ("%s %s\n", PACKAGE, PACKAGE_VERSION);
+      return 0;
+    }
+
+  return -1;
+}
+
+static void
+bjb_application_startup (GApplication *application)
+{
+  BjbApplication *self = (BjbApplication *)application;
+
+  G_APPLICATION_CLASS (bjb_application_parent_class)->startup (application);
+
+  g_action_map_add_action_entries (G_ACTION_MAP (application),
+                                   application_entries,
+                                   G_N_ELEMENTS (application_entries),
+                                   application);
+
+  bjb_application_load_providers (self);
+}
+
+static gint
+bjb_application_command_line (GApplication            *application,
+                              GApplicationCommandLine *command_line)
+{
+  GVariantDict *options;
+
+  options = g_application_command_line_get_options_dict (command_line);
+
+  if (g_variant_dict_contains (options, "quit"))
+    {
+      g_application_quit (application);
+      return 0;
+    }
+
+  g_application_activate (application);
+
+  return 0;
+}
+
+static void
+bjb_application_activate (GApplication *application)
+{
+  GtkWindow *window;
+
+  window = gtk_application_get_active_window (GTK_APPLICATION (application));
+
+  if (window == NULL)
+    window = GTK_WINDOW (bjb_window_new (BJB_APPLICATION (application)));
+
+  gtk_window_present (window);
+}
+
+static void
+bjb_application_class_init (BjbApplicationClass *klass)
+{
+  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
+
+  application_class->handle_local_options = bjb_application_handle_local_options;
+  application_class->startup = bjb_application_startup;
+  application_class->command_line = bjb_application_command_line;
+  application_class->activate = bjb_application_activate;
+
+  /**
+   * BjbApplication::device-added:
+   * @self: a #BjbApplication
+   * @provider: a #BjbProvider
+   *
+   * The "provider-added" signal is emitted when a new provider
+   * is added and ready for use.
+   *
+   * Since: 3.28
+   */
+  signals [PROVIDER_ADDED] =
+    g_signal_new ("provider-added",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL,
+                  g_cclosure_marshal_VOID__OBJECT,
+                  G_TYPE_NONE, 1, BJB_TYPE_PROVIDER);
+  g_signal_set_va_marshaller (signals [PROVIDER_ADDED],
+                              G_TYPE_FROM_CLASS (klass),
+                              g_cclosure_marshal_VOID__OBJECTv);
+
+  main_thread = g_thread_self ();
+  settings = g_settings_new ("org.gnome.bijiben");
+}
+
+static void
+bjb_application_init (BjbApplication *self)
+{
+  g_application_add_main_option_entries (G_APPLICATION (self), cmd_options);
+
+  self->providers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+  g_set_application_name (_("GNOME Notes"));
+  gtk_window_set_default_icon_name ("org.gnome.bijiben");
+}
+
+BjbApplication *
+bjb_application_new (void)
+{
+  return g_object_new (BJB_TYPE_APPLICATION,
+                       "application-id", "org.gnome.bijiben",
+                       "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
+                       NULL);
+}
+
+/**
+ * bjb_application_get_default_settings:
+ *
+ * Get the default #GSettings.
+ *
+ * Returns: (transfer none): A #GSettings
+ */
+GSettings *
+bjb_application_get_default_settings (void)
+{
+  return settings;
+}
+
+/**
+ * bjb_application_get_main_thread:
+ *
+ * Returns the thread-id of the main thread. Useful to check if
+ * the current is the main UI thread or not.
+ * This is used by BJB_IS_MAIN_THREAD() macro.
+ *
+ * Returns: (transfer none): a #GThread
+ */
+GThread *
+bjb_application_get_main_thread (void)
+{
+  return main_thread;
+}
diff --git a/src/bjb-app.h b/src/bjb-app.h
new file mode 100644
index 0000000..b1c2c58
--- /dev/null
+++ b/src/bjb-app.h
@@ -0,0 +1,36 @@
+/* bjb-application.h
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_APPLICATION (bjb_application_get_type ())
+#define BJB_IS_MAIN_THREAD() (g_thread_self () == bjb_application_get_main_thread ())
+
+G_DECLARE_FINAL_TYPE (BjbApplication, bjb_application, BJB, APPLICATION, GtkApplication)
+
+BjbApplication *bjb_application_new                  (void);
+GSettings      *bjb_application_get_default_settings (void);
+GThread        *bjb_application_get_main_thread      (void);
+
+G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]