Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 122 lines (103 sloc) 3.321 kB
2d36ed4 @hluk first commit
authored
1 /*
6fd8944 @hluk Doxygen documentation (incomplete) and code cleaned
authored
2 Copyright (c) 2012, Lukas Holecek <hluk@email.cz>
2d36ed4 @hluk first commit
authored
3
9bd4fb4 @hluk changed item viewer and editor (multiline)
authored
4 This file is part of CopyQ.
2d36ed4 @hluk first commit
authored
5
6 CopyQ is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 CopyQ is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with CopyQ. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "mainwindow.h"
6b4ced7 @hluk UPDATED: QtSingleApplication replaced with QLocalSocket/QLocalServer
authored
21
709ccee @hluk serialize/deserialize, parse arguments in separate module
authored
22 #include "client_server.h"
6b4ced7 @hluk UPDATED: QtSingleApplication replaced with QLocalSocket/QLocalServer
authored
23 #include "clipboardserver.h"
24 #include "clipboardclient.h"
97f9084 @hluk clipboard monitor as a process (if fails, it is restarted); code clea…
authored
25 #include "clipboardmonitor.h"
11731a2 @hluk Scripting using QtScript
authored
26 #include "scriptable.h"
6b4ced7 @hluk UPDATED: QtSingleApplication replaced with QLocalSocket/QLocalServer
authored
27
2d36ed4 @hluk first commit
authored
28 #include <QSettings>
11731a2 @hluk Scripting using QtScript
authored
29 #include <QScriptEngine>
da34f8a @hluk serialize/deserialize client arguments as Base64 encoded string; mino…
authored
30 #include <iostream>
2d36ed4 @hluk first commit
authored
31
7cd64f2 @hluk fixed console output in Windows
authored
32 #ifdef Q_WS_WIN
33 #include <windows.h>
34 #endif
35
11731a2 @hluk Scripting using QtScript
authored
36 static void evaluate(const QString &functionName, const char *arg)
da34f8a @hluk serialize/deserialize client arguments as Base64 encoded string; mino…
authored
37 {
11731a2 @hluk Scripting using QtScript
authored
38 int argc = 0;
39 char *argv[] = {NULL};
40 QApplication app(argc, argv);
41
42 Scriptable scriptable(NULL, NULL);
43 QScriptEngine engine;
44 engine.setGlobalObject( engine.newQObject(&scriptable) );
45
46 QScriptValue fn = engine.globalObject().property(functionName);
47 QScriptValueList fnArgs;
48 if (arg != NULL)
49 fnArgs << QString(arg);
50 std::cout << fn.call(QScriptValue(), fnArgs).toString().toStdString();
da34f8a @hluk serialize/deserialize client arguments as Base64 encoded string; mino…
authored
51 }
52
f9b27c5 @hluk memory leak (function XCloseDisplay not called); cleaned
authored
53 static int startServer(int argc, char *argv[])
54 {
0108f4c @hluk Portability, bugs, block socket signals while reading
authored
55 ClipboardServer app(argc, argv);
56 if ( app.isListening() ) {
7cd64f2 @hluk fixed console output in Windows
authored
57 #ifdef Q_WS_WIN
58 // FIXME: console window is still shown for a moment
59 FreeConsole();
60 #endif
02cd2b0 @hluk safer message passing; some bugs
authored
61 return app.exec();
f9b27c5 @hluk memory leak (function XCloseDisplay not called); cleaned
authored
62 } else {
63 log( QObject::tr("CopyQ server is already running."), LogWarning );
64 return 0;
65 }
66 }
67
68 static int startMonitor(int argc, char *argv[])
69 {
70 ClipboardMonitor app(argc, argv);
31d98d7 @hluk code cleaned and some bugs removed
authored
71 return app.isConnected() ? app.exec() : 0;
f9b27c5 @hluk memory leak (function XCloseDisplay not called); cleaned
authored
72 }
73
74 static int startClient(int argc, char *argv[])
75 {
76 ClipboardClient app(argc, argv);
77 return app.exec();
78 }
79
11731a2 @hluk Scripting using QtScript
authored
80 static bool needsHelp(const char *arg)
81 {
82 return strcmp("-h",arg) == 0 ||
83 strcmp("--help",arg) == 0 ||
84 strcmp("help",arg) == 0;
85 }
86
87 static bool needsVersion(const char *arg)
88 {
89 return strcmp("-v",arg) == 0 ||
90 strcmp("--version",arg) == 0 ||
91 strcmp("version",arg) == 0;
92 }
93
2d36ed4 @hluk first commit
authored
94 int main(int argc, char *argv[])
95 {
11731a2 @hluk Scripting using QtScript
authored
96 // print version
97 if (argc == 2 || argc == 3) {
98 const char *arg = argv[1];
99 if ( argc == 2 && needsVersion(arg) ) {
100 evaluate("version", NULL);
101 return 0;
102 } else if ( needsHelp(arg) ) {
103 evaluate("help", argc == 3 ? argv[2] : NULL);
da34f8a @hluk serialize/deserialize client arguments as Base64 encoded string; mino…
authored
104 return 0;
105 }
106 }
9768c84 @hluk UPDATED: QtSingleApplication; peer used to communicate with server/cl…
authored
107
108 if (argc == 1) {
f9b27c5 @hluk memory leak (function XCloseDisplay not called); cleaned
authored
109 // if server hasn't been run yet and no argument were specified
110 // then run this as server
111 return startServer(argc, argv);
112 } else if (argc == 2 && strcmp(argv[1], "monitor") == 0) {
113 // if argument specified and server is running
114 // then run this as client
115 return startMonitor(argc, argv);
116 } else {
117 // if argument specified and server is running
118 // then run this as client
119 return startClient(argc, argv);
97f9084 @hluk clipboard monitor as a process (if fails, it is restarted); code clea…
authored
120 }
2d36ed4 @hluk first commit
authored
121 }
Something went wrong with that request. Please try again.