File: application.cpp

package info (click to toggle)
gobby 0.5.0-8.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,836 kB
  • ctags: 2,548
  • sloc: cpp: 19,597; sh: 4,337; xml: 456; makefile: 398
file content (230 lines) | stat: -rw-r--r-- 6,287 bytes parent folder | download
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
/* Gobby - GTK-based collaborative text editor
 * Copyright (C) 2008-2014 Armin Burgmeier <armin@arbur.net>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "application.hpp"
#include "features.hpp"

#include "util/i18n.hpp"
#include "util/file.hpp"

#include <iostream>

#include <libinfinity/common/inf-init.h>

#include <libintl.h>

namespace
{
	std::string gobby_localedir()
	{
#ifdef G_OS_WIN32
		gchar* root =
			g_win32_get_package_installation_directory_of_module(
				NULL);

		gchar* temp = g_build_filename(root, "share", "locale", NULL);
		g_free(root);

		gchar* result = g_win32_locale_filename_from_utf8(temp);
		g_free(temp);

		std::string cpp_result(result);
		g_free(result);

		return cpp_result;
#else
		return GOBBY_LOCALEDIR;
#endif
	}
}

class Gobby::Application::Data
{
public:
	Data();
	~Data();

	// TODO: Does the config object really need to stay around, or can
	// it be thrown away after we have loaded the preferences?
	Config config;
	Preferences preferences;
	CertificateManager certificate_manager;
	IconManager icon_manager; // TODO: Switch to icon names
};

Gobby::Application::Data::Data():
	config(config_filename("config.xml")),
	preferences(config),
	certificate_manager(preferences)
{
}

Gobby::Application::Data::~Data()
{
	preferences.serialize(config);
}

Gobby::Application::Application():
	Gtk::Application("de._0x539.gobby",
	                 Gio::APPLICATION_HANDLES_OPEN)
{
	setlocale(LC_ALL, "");
	textdomain(GETTEXT_PACKAGE);
	bindtextdomain(GETTEXT_PACKAGE, gobby_localedir().c_str());
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");

	// There is no on_handle_local_options default handler in
	// Gio::Application:
	signal_handle_local_options().connect(
		sigc::mem_fun(*this, &Application::on_handle_local_options),
		false);

	// TODO: Here, probably N_(...) should be used for the translatable
	// strings, so that the option entry array can be static. However,
	// in this case the gettext translation domain cannot be set.
	// c.f. bugzilla.gnome.org #736637.
	// TODO: The only purpose of the G_OPTION_REMAINING option is to show
	// the arg_description in the --help output. However, in that case
	// the "open" signal is not emitted, since Glib assumes this is just
	// another option that we are handling ourselves. We could work around
	// it by handling the "command-line" signal instead of the "open"
	// signal.
	const GOptionEntry entries[] = {
		{ "version", 'v', 0, G_OPTION_ARG_NONE, NULL,
		  _("Display version information and exit"), NULL
		}, { "new-instance", 'n', 0, G_OPTION_ARG_NONE, NULL,
		  _("Start a new gobby instance also if there is one "
		     "already running"), NULL
		/*}, { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
		     NULL, NULL, N_("[FILE1 or URI1] [FILE2 or URI2] [...]")
		*/}, { NULL }
	};

	g_application_add_main_option_entries(G_APPLICATION(gobj()), entries);
}

Glib::RefPtr<Gobby::Application> Gobby::Application::create()
{
	return Glib::RefPtr<Gobby::Application>(
		new Application);
}

int Gobby::Application::on_handle_local_options(
	const Glib::RefPtr<Glib::VariantDict>& options_dict)
{
	bool display_version;
	if(options_dict->lookup_value("version", display_version))
	{
		std::cout << "Gobby " << PACKAGE_VERSION << std::endl;
		return 0;
	}

	bool new_instance;
	if(options_dict->lookup_value("new-instance", new_instance))
	{
		set_flags(get_flags() | Gio::APPLICATION_NON_UNIQUE);
		options_dict->remove("new-instance");
	}

	// Continue normal processing
	return -1;
}

void Gobby::Application::on_startup()
{
	Gtk::Application::on_startup();

	try
	{
		Gtk::Window::set_default_icon_name("gobby-0.5");

		GError* error = NULL;
		if(inf_init(&error) != TRUE)
			throw Glib::Error(error);

		// Allocate the per-application data. This cannot be
		// done earlier, such as in the contrutor, since we only
		// need to do it if we are the primary instance.
		m_data.reset(new Data);

		m_gobby_window = new Gobby::Window(
			m_data->config, m_data->preferences,
			m_data->icon_manager, m_data->certificate_manager);

		m_window.reset(m_gobby_window);
		m_window->show();
		add_window(*m_window);
	}
	catch(const Glib::Exception& ex)
	{
		handle_error(ex.what());
	}
	catch(const std::exception& ex)
	{
		handle_error(ex.what());
	}
}

void Gobby::Application::on_activate()
{
	if(m_window.get())
		m_window->present();

	Gtk::Application::on_activate();
}

void Gobby::Application::on_open(const type_vec_files& files,
                                 const Glib::ustring& hint)
{
	Gtk::Application::on_open(files, hint);

	// If we don't have a window we ignore the file open request. This can
	// for example happen when Gobby was launched with command line
	// arguments, but the Gobby initialization failed.
	if(!m_gobby_window)
		return;

	Operations::uri_list uris;
	for(type_vec_files::const_iterator iter = files.begin();
	    iter != files.end(); ++iter)
	{
		Glib::RefPtr<Gio::File> file = *iter;
		if(file->get_uri_scheme() == "infinote")
			m_gobby_window->subscribe(file->get_uri());
		else
			uris.push_back(file->get_uri());
	}

	if(!uris.empty())
		m_gobby_window->open_files(uris);
}

void Gobby::Application::handle_error(const std::string& message)
{
	std::auto_ptr<Gtk::MessageDialog> dialog(
		new Gtk::MessageDialog(
			"Failed to startup Gobby", false,
			Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, false));
	dialog->signal_response().connect(
		sigc::hide(sigc::mem_fun(*dialog, &Gtk::Window::hide)));

	dialog->set_title("Gobby");
	dialog->set_secondary_text(message);
	m_window = dialog;

	m_window->show();
	add_window(*m_window);
}