summaryrefslogtreecommitdiff
path: root/src/gui_app.c
blob: a910ebe7d01821cd8b4608b0e9c2c840ea3f3148 (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
/* Camicro - Microscope camera viewer.
 *
 * Copyright (C) 2019 Nicolas Schodet
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Contact :
 *        Web: http://ni.fr.eu.org/
 *      Email: <nico at ni.fr.eu.org>
 */
#include "gui_app.h"

#include "device_manager.h"
#include "gui_app_window.h"
#include "options.h"
#include "utils.h"

struct _GuiApp {
    GtkApplication parent;
};

typedef struct _GuiAppPrivate GuiAppPrivate;

struct _GuiAppPrivate {
    struct device_manager *device_manager;
};

G_DEFINE_TYPE_WITH_PRIVATE(GuiApp, gui_app, GTK_TYPE_APPLICATION)

static void
gui_app_device_manager_cb(gpointer user_data)
{
    GuiApp *app = user_data;
    g_signal_emit_by_name(app, "video-ready");
}

static void
gui_app_init(GuiApp *app)
{
    GuiAppPrivate *priv = gui_app_get_instance_private(app);
    priv->device_manager = NULL;
    options_add(G_APPLICATION(app));
}

static void
gui_app_startup(GApplication *app)
{
    G_APPLICATION_CLASS(gui_app_parent_class)->startup(app);
    GuiAppPrivate *priv = gui_app_get_instance_private(GUI_APP(app));
    priv->device_manager = device_manager_new(NULL);
    device_manager_set_callback(priv->device_manager,
	    gui_app_device_manager_cb, app);
}

static void
gui_app_activate(GApplication *app)
{
    GuiAppPrivate *priv = gui_app_get_instance_private(GUI_APP(app));
    GError *error = NULL;
    struct device *device = device_manager_open(priv->device_manager, &error);
    if (!device) {
	utils_dialog_error(NULL, "unable to find device: %s", error->message);
	g_error_free(error);
    } else {
	GuiAppWindow *window;
	window = gui_app_window_new(GUI_APP(app));
	gui_app_window_open(window, device);
	gtk_window_present(GTK_WINDOW(window));
    }
}

static void
gui_app_shutdown(GApplication *app)
{
    GuiAppPrivate *priv = gui_app_get_instance_private(GUI_APP(app));
    if (priv->device_manager)
	device_manager_destroy(priv->device_manager);
    G_APPLICATION_CLASS(gui_app_parent_class)->shutdown(app);
}

static void
gui_app_class_init(GuiAppClass *class)
{
    G_APPLICATION_CLASS(class)->startup = gui_app_startup;
    G_APPLICATION_CLASS(class)->activate = gui_app_activate;
    G_APPLICATION_CLASS(class)->shutdown = gui_app_shutdown;
    G_APPLICATION_CLASS(class)->handle_local_options = options_handle;
    g_signal_new("video-ready", GUI_APP_TYPE, G_SIGNAL_RUN_FIRST, 0, NULL,
	    NULL, NULL, G_TYPE_NONE, 0);
}

GuiApp *
gui_app_new(void)
{
  return g_object_new(GUI_APP_TYPE,
	  "application-id", "org.eu.fr.ni.Camicro",
	  NULL);
}