Commit d81ef291 authored by Stepan Salenikovich's avatar Stepan Salenikovich

gnome: subclassed GtkApplication

Refs #66540

Change-Id: I28a7d7c62c239ff976b4df2706419bcefe05aceb
parent e3500cbd
......@@ -102,6 +102,10 @@ ENDIF()
# source files to compile
SET( SRC_FILES
src/main.cpp
src/ring_client.h
src/ring_client.cpp
src/ring_client_options.h
src/ring_client_options.c
)
# IF(${ENABLE_TEST} MATCHES true)
......@@ -112,6 +116,6 @@ SET( SRC_FILES
# FIND_PACKAGE ( Gettext REQUIRED )
# ADD_SUBDIRECTORY( po )
ADD_EXECUTABLE(ring-client-gnome ${SRC_FILES})
ADD_EXECUTABLE(ring-gnome ${SRC_FILES})
TARGET_LINK_LIBRARIES(ring-client-gnome ${LIB_RING_CLIENT_LIBRARY} ${GTK3_LIBRARIES} Qt5::Core)
TARGET_LINK_LIBRARIES(ring-gnome ${LIB_RING_CLIENT_LIBRARY} ${GTK3_LIBRARIES} Qt5::Core)
/*
* Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
* Copyright (C) 2015 Savoir-Faire Linux Inc.
* Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
......@@ -29,39 +29,10 @@
*/
#include <gtk/gtk.h>
#include <QCoreApplication>
#include <callmodel.h>
#include "ring_client.h"
int
main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init (&argc, &argv);
// enable debug
g_setenv("G_MESSAGES_DEBUG", "all", TRUE);
g_debug("debug enabled");
QCoreApplication *app = NULL;
try {
app = new QCoreApplication(argc, argv);
CallModel::instance();
} catch (const char * msg) {
g_debug("caught error: %s\n", msg);
return 1;
}
// create an empty window
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Ring");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
delete app;
return 0;
return g_application_run(G_APPLICATION(ring_client_new()), argc, argv);
}
/*
* Copyright (C) 2015 Savoir-Faire Linux Inc.
* Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "ring_client.h"
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <QCoreApplication>
#include <QString>
#include <QByteArray>
#include <callmodel.h>
#include "ring_client_options.h"
struct _RingClientPrivate {
/* main window */
GtkWidget *win;
/* for libRingclient */
QCoreApplication *qtapp;
};
G_DEFINE_TYPE(RingClient, ring_client, GTK_TYPE_APPLICATION);
#define RING_CLIENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_CLIENT_TYPE, RingClientPrivate))
static void
init_exception_dialog(const char* msg)
{
g_warning("%s", msg);
GtkWidget *dialog = gtk_message_dialog_new(NULL,
(GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
_("Unable to initialize.\nMake sure the Ring daemon (dring) is running.\nError: %s"),
msg);
gtk_window_set_title(GTK_WINDOW(dialog), _("Ring Error"));
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
static int
ring_client_command_line(GApplication *app, GApplicationCommandLine *cmdline)
{
RingClient *client = RING_CLIENT(app);
RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(client);
gint argc;
gchar **argv = g_application_command_line_get_arguments(cmdline, &argc);
GOptionContext *context = ring_client_options_get_context();
GError *error = NULL;
if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
error->message, argv[0]);
g_error_free(error);
g_option_context_free(context);
return 1;
}
g_option_context_free(context);
/* Override theme since we don't have appropriate icons for a dark them (yet) */
GtkSettings *gtk_settings = gtk_settings_get_default();
g_object_set(G_OBJECT(gtk_settings), "gtk-application-prefer-dark-theme",
FALSE, NULL);
/* init libRingClient and make sure its connected to the dbus */
try {
/* TODO: do we care about passing the cmd line arguments here? */
priv->qtapp = new QCoreApplication(argc, argv);
/* the call model will try to connect to dring via dbus */
CallModel::instance();
} catch (const char * msg) {
init_exception_dialog(msg);
return 1;
} catch(QString& msg) {
QByteArray ba = msg.toLocal8Bit();
const char *c_str = ba.data();
init_exception_dialog(c_str);
return 1;
}
/* create an empty window */
priv->win = gtk_application_window_new(GTK_APPLICATION(app));
gtk_window_present(GTK_WINDOW(priv->win));
return 0;
}
static void
ring_client_shutdown(GApplication *app)
{
RingClient *self = RING_CLIENT(app);
RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
/* free the QCoreApplication, which will destroy all libRingClient models
* and thus send the Unregister signal over dbus to dring */
delete priv->qtapp;
/* Chain up to the parent class */
G_APPLICATION_CLASS(ring_client_parent_class)->shutdown(app);
}
static void
ring_client_init(RingClient *self)
{
RingClientPrivate *priv = RING_CLIENT_GET_PRIVATE(self);
/* init widget */
priv->win = NULL;
priv->qtapp = NULL;
}
static void
ring_client_class_init(RingClientClass *klass)
{
g_debug("ring_client_class_init()");
g_type_class_add_private(klass, sizeof(RingClientPrivate));
G_APPLICATION_CLASS(klass)->command_line = ring_client_command_line;
G_APPLICATION_CLASS(klass)->shutdown = ring_client_shutdown;
}
RingClient *
ring_client_new()
{
return (RingClient *)g_object_new(ring_client_get_type(),
"application-id", "cx.ring.RingGnome",
"flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL);
}
/*
* Copyright (C) 2015 Savoir-Faire Linux Inc.
* Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#ifndef RING_CLIENT_H_
#define RING_CLIENT_H_
#include <gtk/gtk.h>
// #define RING_GSETTINGS_SCHEMA "cx.ring.RingGnome"
#define RING_CLIENT_TYPE (ring_client_get_type())
#define RING_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), RING_CLIENT_TYPE, RingClient))
typedef struct _RingClientPrivate RingClientPrivate;
typedef struct _RingClientClass RingClientClass;
typedef struct _RingClient RingClient;
struct _RingClientClass
{
GtkApplicationClass parent_class;
};
struct _RingClient
{
GtkApplication parent;
/* Private */
RingClientPrivate *priv;
};
/* Public interface */
GType ring_client_get_type(void) G_GNUC_CONST;
RingClient *ring_client_new(void);
#endif /* RING_CLIENT_H_ */
/*
* Copyright (C) 2013-2015 Savoir-Faire Linux Inc.
* Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "ring_client_options.h"
/* TODO: add header for auto generated config
* #include "config.h"
*/
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <stdlib.h>
G_GNUC_NORETURN static gboolean
option_version_cb(G_GNUC_UNUSED const gchar *option_name,
G_GNUC_UNUSED const gchar *value,
G_GNUC_UNUSED gpointer data,
G_GNUC_UNUSED GError **error)
{
/* TODO: replace with auto generated version */
g_print("%s\n", "0.1");
exit(EXIT_SUCCESS);
}
static gboolean
option_debug_cb(G_GNUC_UNUSED const gchar *option_name,
G_GNUC_UNUSED const gchar *value,
G_GNUC_UNUSED gpointer data,
G_GNUC_UNUSED GError **error)
{
g_setenv("G_MESSAGES_DEBUG", "all", TRUE);
g_debug("debug enabled");
return TRUE;
}
static const GOptionEntry all_options[] = {
{"version", 'v', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL},
{"debug", 'd', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, option_debug_cb, "Enable debug", NULL},
{NULL} /* list must be NULL-terminated */
};
GOptionContext *
ring_client_options_get_context()
{
/* TODO: for some reason the given description and added options aren't printed
* when '--help' is invoked... possibly a GTK bug.
*/
GOptionContext *context = g_option_context_new("- GNOME client for Ring");
/* TODO: add translation domain */
g_option_context_add_main_entries(context, all_options, NULL);
g_option_context_add_group(context, gtk_get_option_group(TRUE));
return context;
}
/*
* Copyright (C) 2013-2015 Savoir-Faire Linux Inc.
* Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#ifndef RING_CLIENT_OPTIONS_H_
#define RING_CLIENT_OPTIONS_H_
#include <glib.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
GOptionContext *ring_client_options_get_context(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* RING_CLIENT_OPTIONS_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment