File: main.cpp

package info (click to toggle)
x2godesktopsharing 3.2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 1,948 kB
  • sloc: cpp: 1,064; makefile: 29; sh: 7
file content (169 lines) | stat: -rw-r--r-- 4,794 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
/***************************************************************************
 *   Copyright (C) 2006-2018 by Oleksandr Shneyder                         *
 *   o.shneyder@phoca-gmbh.de                                              *
 *                                                                         *
 *   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.             *
 ***************************************************************************/


#include <QApplication>
#include <QTranslator>
#include <QLocale>
#include "sharetray.h"
#include <QLocalSocket>
#include <iostream>
#include <QDir>
#include <csignal>

using namespace std;

void  client ( const QStringList & cmd )
{

	if ( cmd.size() !=11 )
	{
		cerr<<"wrong parameters"<<endl;
		cout<<"DENY";
		return;
	}
	QStringList params=cmd[9].split ( "XSHAD" );
	if ( params.size() !=3 )
	{
		cerr<<"wrong parameters"<<endl;
		cout<<"DENY";
		return;
	}

	cerr<<"starting x2godesktopsharing in client mode, cmd: "<<
	cmd.join(" ").toLatin1().data() <<std::endl;

	QString dispname=params[2];
	// QString socketName=QDir::tempPath() +"/x2godesktopsharing_@"+
	//                   params[1]+"@"+dispname;
	QString socketName="/tmp/x2godesktopsharing_@" + params[1]+"@"+dispname;
	QLocalSocket sock;
	sock.connectToServer ( socketName );
	if ( !sock.waitForConnected ( 3000 ) )
	{
		QString message="Unable to connect: " +socketName;
		cout<<"DENY";
		cerr<<message.toLatin1().data() <<endl;
		return;
	}
	sock.write ( cmd.join(" ").toLatin1().data(),cmd.join(" ").toLatin1().length() );
	if ( !sock.waitForReadyRead() )
	{
		cout<<"DENY";
		cerr<<"Cannot write to socket: "<<socketName.toLatin1().data() <<endl;
		return;;
	}
	char buffer[256];
	int read=sock.read ( buffer,255 );
	if ( read<=0 )
	{
		cout<<"DENY";
		cerr<<"Cannot read from socket: " <<socketName.toLatin1().data() <<endl;
		return;
	}
	buffer[read]=0;
	cout<<buffer<<endl;
}

static int setup_unix_signal_handlers()
{
	struct sigaction keybint, term, abort, hup;

	keybint.sa_handler = ShareTray::keybintSignalHandler;
	sigemptyset(&keybint.sa_mask);
	keybint.sa_flags = 0;
	keybint.sa_flags |= SA_RESTART;

	if (sigaction(SIGINT, &keybint, 0) > 0)
		return 1;

	term.sa_handler = ShareTray::termSignalHandler;
	sigemptyset(&term.sa_mask);
	term.sa_flags |= SA_RESTART;

	if (sigaction(SIGTERM, &term, 0) > 0)
	return 2;

	abort.sa_handler = ShareTray::abortSignalHandler;
	sigemptyset(&abort.sa_mask);
	abort.sa_flags |= SA_RESTART;

	if (sigaction(SIGABRT, &abort, 0) > 0)
	return 3;

	hup.sa_handler = ShareTray::hupSignalHandler;
	sigemptyset(&hup.sa_mask);
	hup.sa_flags |= SA_RESTART;

	if (sigaction(SIGHUP, &hup, 0) > 0)
	return 4;

	return 0;
}

int main ( int argc, char *argv[] )
{
	if ( argc>2 )
	{
		QString par=argv[1];
		if ( par == "client" )
		{
			QStringList cmdl;
			for ( int i=2; i<argc;++i )
			cmdl<<argv[i];
			client ( cmdl );
			return 0;
		}
	}

	QApplication app ( argc,argv );
	QStringList args=QCoreApplication::arguments();

	QTranslator translator;
	QString filename=QString ( ":/x2godesktopsharing_%1" ).arg (
	                 QLocale::system().name() );
	filename=filename.toLower();
	if ( !translator.load ( filename ) )
	{
		qDebug ( "Can't load translator (%s) !\n",
		         filename.toLocal8Bit().data() );
	}
	else
		app.installTranslator ( &translator );

	QTranslator qtTranslator;
	filename=QString ( ":/qt_%1" ).arg ( QLocale::system().name() );
	if ( !qtTranslator.load ( filename ) )
	{
		qDebug ( "Can't load translator (%s) !\n",
		         filename.toLocal8Bit().data() );
	}
	else
		app.installTranslator ( &qtTranslator );

	ShareTray* tray=new ShareTray;
	tray->hide();
	setup_unix_signal_handlers();
	try {
		return app.exec();
	} catch (const std::bad_alloc &) {
		tray->slotMenuClose();
	}
}