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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/* 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 "features.hpp"
#include "window.hpp"
#include "core/iconmanager.hpp"
#include "util/config.hpp"
#include "util/file.hpp"
#include "util/i18n.hpp"
#include <libinfinity/common/inf-init.h>
#include <gtkmm/main.h>
#include <gtkmm/messagedialog.h>
#include <giomm/init.h>
#include <glibmm/optionentry.h>
#include <glibmm/optiongroup.h>
#include <glibmm/optioncontext.h>
#ifdef WITH_UNIQUE
# include <unique/unique.h>
#endif
#include <libintl.h> // bindtextdomain
#include <iostream>
#include <vector>
namespace
{
void handle_exception(const Glib::ustring& message)
{
Gtk::MessageDialog dlg("Unhandled exception", false,
Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
dlg.set_secondary_text(message);
dlg.run();
std::cerr << "Unhandled exception: " << message << std::endl;
}
const char* _(const char* str)
{
return Gobby::_(str);
}
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
}
#ifdef WITH_UNIQUE
int send_message_with_uris(UniqueApp* app,
gint message_id,
const std::vector<Glib::ustring>& uris)
{
std::vector<const gchar*> uri_cstrs(uris.size() + 1);
for(unsigned int i = 0; i < uris.size(); ++i)
uri_cstrs[i] = uris[i].c_str();
UniqueMessageData* message = unique_message_data_new();
unique_message_data_set_uris(
message, const_cast<gchar**>(&uri_cstrs[0]));
UniqueResponse response = unique_app_send_message(
app, message_id, message);
unique_message_data_free(message);
if(response == UNIQUE_RESPONSE_OK)
{
return 0;
}
else
{
std::cerr
<< "error sending URIs to existing gobby "
"instance (libunique): "
<< static_cast<int>(response)
<< std::endl;
return -1;
}
}
int my_unique_activate(UniqueApp* app) {
UniqueResponse response =
unique_app_send_message(app, UNIQUE_ACTIVATE, NULL);
if(response != UNIQUE_RESPONSE_OK)
{
std::cerr
<< "error activating existing gobby "
"instance (libunique): "
<< static_cast<int>(response)
<< std::endl;
return -1;
}
else
{
return 0;
}
}
int my_unique_send_file_args(UniqueApp* app,
int argc,
const char* const* argv)
{
std::vector<Glib::ustring> uris(argc);
for(int i = 0; i < argc; ++i)
{
uris[i] = Gio::File::create_for_commandline_arg(
argv[i])->get_uri();
}
return send_message_with_uris(app, UNIQUE_OPEN, uris);
}
int my_unique_send_hostname_args(
UniqueApp* app,
const std::vector<Glib::ustring>& hostnames)
{
std::vector<Glib::ustring> uris(hostnames);
for(unsigned int i = 0; i < uris.size(); ++i)
{
uris[i].insert(0, "infinote://");
}
return send_message_with_uris(
app, Gobby::UNIQUE_GOBBY_CONNECT, uris);
}
int my_unique_check_other(UniqueApp* app,
int argc, const char* const* argv,
const std::vector<Glib::ustring>& hostnames)
{
if(argc == 0 && hostnames.empty())
{
return my_unique_activate(app);
}
if(argc) {
if(my_unique_send_file_args(app, argc, argv) != 0)
return -1;
}
if(!hostnames.empty()) {
if (my_unique_send_hostname_args(app, hostnames))
return -1;
}
return 0;
}
#endif // WITH_UNIQUE
}
int main(int argc, char* argv[]) try
{
g_thread_init(NULL);
Gio::init();
setlocale(LC_ALL, "");
bindtextdomain(GETTEXT_PACKAGE, gobby_localedir().c_str());
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
bool new_instance = false;
bool display_version = false;
std::vector<Glib::ustring> hostnames;
Glib::OptionGroup opt_group_gobby("gobby",
_("Gobby options"), _("Options related to Gobby"));
Glib::OptionEntry opt_version;
opt_version.set_short_name('v');
opt_version.set_long_name("version");
opt_version.set_description(
_("Display version information and exit"));
opt_group_gobby.add_entry(opt_version, display_version);
Glib::OptionEntry opt_new_instance;
opt_new_instance.set_short_name('n');
opt_new_instance.set_long_name("new-instance");
opt_new_instance.set_description(
_("Also start a new Gobby instance when there is one "
"running already"));
opt_group_gobby.add_entry(opt_new_instance, new_instance);
Glib::OptionEntry opt_connect;
opt_connect.set_short_name('c');
opt_connect.set_long_name("connect");
opt_connect.set_description(
_("Connect to given host on startup, can be given multiple times"));
opt_connect.set_arg_description(_("HOSTNAME"));
opt_group_gobby.add_entry(opt_connect, hostnames);
Glib::OptionContext opt_ctx;
opt_ctx.set_help_enabled(true);
opt_ctx.set_ignore_unknown_options(false);
opt_ctx.set_main_group(opt_group_gobby);
// I would rather like to have Gtk::Main on the stack, but I see
// no other chance to catch exceptions from the command line option
// parsing. armin.
// TODO: Maybe we should parse before initializing GTK+, using
// Gtk::Main::add_gtk_option_group() with open_default_display set
// to false.
std::auto_ptr<Gtk::Main> kit;
try
{
kit.reset(new Gtk::Main(argc, argv, opt_ctx));
}
catch(Glib::Exception& e)
{
// Protect for non-UTF8 command lines (GTK probably already
// converts to UTF-8 in case the system locale is not UTF-8,
// but it can happen that input is given not in the system
// locale, or simply invalid UTF-8. In that case, printing
// e.what() on stdout would throw another exception, which we
// want to avoid here, because otherwise we would want to
// show that exception in an error dialog, but GTK+ failed
// to initialize.
if(e.what().validate())
std::cerr << e.what() << std::endl;
else
std::cerr << "Invalid input on command line" << std::endl;
return EXIT_FAILURE;
}
if(display_version)
{
std::cout << "Gobby " << PACKAGE_VERSION << std::endl;
return EXIT_SUCCESS;
}
#ifdef WITH_UNIQUE
UniqueApp* app = unique_app_new_with_commands(
"de._0x539.gobby", NULL,
"UNIQUE_GOBBY_CONNECT", Gobby::UNIQUE_GOBBY_CONNECT,
NULL);
if(!new_instance && unique_app_is_running(app))
{
int exit_code = my_unique_check_other(
app,
argc - 1, argv + 1,
hostnames);
g_object_unref(app);
return exit_code;
}
#endif // WITH_UNIQUE
GError* error = NULL;
if(!inf_init(&error))
{
std::string message = error->message;
g_error_free(error);
throw std::runtime_error(message);
}
// Read the configuration
Gobby::Config config(Gobby::config_filename("config.xml"));
Gobby::Preferences preferences(config);
Gobby::CertificateManager cert_manager(preferences);
Gobby::IconManager icon_manager;
// Set default icon
Gtk::Window::set_default_icon_name("gobby-0.5");
// Open a scope here, so that the main window is destructed
// before we serialize the preferences, so that if the window
// sets options at destruction time, they are stored correctly.
{
// Create window
Gobby::Window wnd(
argc-1,
argv+1,
config,
preferences,
icon_manager,
cert_manager
#ifdef WITH_UNIQUE
, app
#endif
);
#ifdef WITH_UNIQUE
g_object_unref(app);
#endif
wnd.show();
for(std::vector<Glib::ustring>::const_iterator i =
hostnames.begin();
i != hostnames.end(); ++ i)
{
wnd.connect_to_host(*i);
}
wnd.signal_hide().connect(sigc::ptr_fun(&Gtk::Main::quit) );
kit->run();
}
preferences.serialize(config);
//inf_deinit();
return 0;
}
catch(Glib::Exception& e)
{
handle_exception(e.what() );
}
catch(std::exception& e)
{
handle_exception(e.what() );
}
|