~vcs-imports/lince/trunk

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
/***************************************************************************
 *            main.cc
 *
 *  Copyright  2008  Fernando TarĂ­n Morales
 *  icemanf@gmail.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
 */

#include <gtkmm/main.h>
#include <glibmmconfig.h>
#include <glibmm/i18n.h>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

#include "../config.h"
#include "Lince/Core.h"
#include "Lince/Utils.h"

#include "Utils.h"
#include "MainWindow.h"
#include "GuiSetupManager.h"

#include <iostream>

using namespace Lince;

bool is_Primary=true;
// This signal will connect the request to add a torrent from another app
// using dbus or from the command line
sigc::signal<void, std::string> sig_to_add_a_torrent_file;
sigc::signal<void, std::string> sig_to_add_a_magnet_link;


class Options : public Glib::OptionGroup
{
public:
	Options();
	~Options();

	bool version, quit;
	std::vector<Glib::ustring> uris;
};

Options::Options()
: Glib::OptionGroup(PACKAGE_NAME, "Command line options"),
	version(false),
	quit(false)
{
	Glib::OptionEntry e_version;
	e_version.set_long_name("version") ;
	e_version.set_description(_("Show version and quit"));
	add_entry(e_version, version);

	Glib::OptionEntry e_remaining;
	e_remaining.set_long_name(G_OPTION_REMAINING);
	add_entry(e_remaining, uris);
}

Options::~Options()
{
}

#if HAVE_DBUS
static DBusConnection* conn;

bool init_dbus()
{
	DBusMessage* msg;
	DBusMessageIter args;
	DBusError err;
	int ret;
	dbus_uint32_t serial = 0;
	
	// initialise the error value
	dbus_error_init(&err);
	
	// connect to the DBUS system bus, and check for errors
	conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
	if (dbus_error_is_set(&err))
	{ 
		std::cerr << "Connection Error (" << err.message << ")" << std::endl;
		dbus_error_free(&err);
	}
	
	if (NULL == conn)
		return false; 
	
	// register our name on the bus, and check for errors
	ret = dbus_bus_request_name(conn, DBUS_NAME_LINCE, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
	if (dbus_error_is_set(&err)) 
	{
		std::cerr << "Connection Error (" << err.message << ")" << std::endl;
		dbus_error_free(&err); 
	}
	
	if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
		return false;
	
	return true;
}

bool send_message(Glib::ustring mess)
{
	DBusMessage *msg;
	DBusMessageIter iter;
	const char * message = mess.c_str();

	msg = dbus_message_new_signal(
          	DBUS_PATH_LINCE,
	        DBUS_INTERFACE_LINCE, "SendUri");

	dbus_message_iter_init_append (msg, &iter);
	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &message);

	if (!dbus_connection_send (conn, msg, NULL))
        	return false;

	dbus_connection_has_messages_to_send(conn);
	dbus_message_unref (msg);
	dbus_connection_flush (conn);

	return true;
}

#else
void set_lockup_file()
{
	boost::filesystem::path p = get_base_directory() / std::string(".lock");
	boost::filesystem::ofstream in(p, std::ios_base::binary);
	in.close();
}

void remove_lockup_file()
{
	boost::filesystem::path p = get_base_directory() / std::string(".lock");
	boost::filesystem::remove(p);
}

bool check_lockup_file()
{
	boost::filesystem::path p = get_base_directory() / std::string(".lock");
	return boost::filesystem::exists(p);
}
#endif

void add_uri(Glib::ustring &uri)
{
	if (Utils::isAMagnetLink(uri))
		sig_to_add_a_magnet_link(uri);
	else
		sig_to_add_a_torrent_file.emit(uri);
}

struct manage_uri : public std::unary_function<void, Glib::ustring>
{
	void operator()(Glib::ustring uri)
	{
		if (!Glib::path_is_absolute(uri) && !Utils::isAMagnetLink(uri))
				uri = Glib::build_filename(Glib::get_current_dir(), uri);

		// Send the files to the primary session.
		if (!is_Primary)
		{
#if HAVE_DBUS
			send_message(uri);
#endif
		}
		// Add the files to the session.
		else
		{
			add_uri(uri);
		}
	}
};

int main(int ac, char* av[]){
	// Set locale translations.
	bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
	textdomain(GETTEXT_PACKAGE);
	
	Gtk::Main kit(ac, av);
	
	if(!Glib::thread_supported()) 
		Glib::thread_init();
	
	Options options;
	//work around for gnomemm bug
	Glib::OptionContext* context = new Glib::OptionContext(
		_("[FILE...] \n\nA BitTorrent client\n"));
	context->set_main_group(options);
	
	try
	{
		context->parse(ac, av);
	}
	catch (const Glib::OptionError& e)
	{
		std::cerr << e.what() << std::endl;
		std::cout << _("Run \"lince --help\" to see a full list of available command line options.\n");
		return 1;
	}
	delete context;
	
	if (options.version)
	{
		std::cout << PACKAGE_VERSION << std::endl;
		return 0;
	}
	
#if HAVE_DBUS
	if (!init_dbus())
	{
		is_Primary = false;
		
		if (!options.uris.empty())
			std::for_each(options.uris.begin(), options.uris.end(), manage_uri());
		else
			Utils::warningDialog(_("Lince is already running"),
							 _("Only one instance of Lince can be executed"));
		return 1;
	}
	
	DBusError error;
	dbus_error_init (&error);

	dbus_bus_add_match (conn, "type='signal', interface='net.p2p.lince.addtorrent'", &error);

	if (dbus_error_is_set(&error))
	{
		std::cerr << "Connection Error (" << error.message << ")" << std::endl;
		dbus_error_free(&error);
	}

#else
	if (check_lockup_file())
	{
		bool ret;
		ret = Utils::askDialog(_("Warning!"),
							   _("Lince is already running"),
							   _("If Lince is not running remove the lock file.\n"
								 "Do you want to remove the lock file?"));
		
		if (ret)
			remove_lockup_file();
			
		return 1;
	}

	// Set that lince is running.
	set_lockup_file();
#endif
		
	Core::init();
	
#if HAVE_DBUS
	MainWindow m(conn);
#else
	MainWindow m;
#endif
	
	// Connect the signal to the proper handler in MainWindow
	sig_to_add_a_torrent_file.connect(sigc::mem_fun(m,
			&MainWindow::load_file_from_request) );

	sig_to_add_a_magnet_link.connect(sigc::mem_fun(m,
			&MainWindow::load_magnet_from_request) );

	if (!options.uris.empty())
		std::for_each(options.uris.begin(), options.uris.end(), manage_uri());
	
	//Gtk::Main::run(m); //Shows the window and returns when it is closed.
	gtk_widget_show_all((GtkWidget*) m.gobj());
 	gtk_main();

	Core::uninit();
	
#if !HAVE_DBUS
	// Remove file lock
	remove_lockup_file();
#endif

	return 0;
}