Changeset View
Changeset View
Standalone View
Standalone View
src/dolphinsinglewindow.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by intika <intika@librefox.org> * | ||||
3 | * Copyright (C) 2018 by intika <intika@librefox.org> * | ||||
4 | * Copyright (C) 2016 by Fady S. Ghatas @ TiTrias * | ||||
5 | * * | ||||
6 | * This program 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 2 of the License, or * | ||||
9 | * (at your option) any later version. * | ||||
10 | * * | ||||
11 | * This program 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 this program; if not, write to the * | ||||
18 | * Free Software Foundation, Inc., * | ||||
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||||
20 | ***************************************************************************/ | ||||
21 | | ||||
22 | #include "dolphinsinglewindow.h" | ||||
23 | #define TIME_OUT (1000) | ||||
24 | #define SERVER_NAME "SingleDolphin" | ||||
25 | DolphinSingleWindow *DolphinSingleWindow::window = 0; | ||||
26 | | ||||
27 | namespace { | ||||
28 | // Used for GeneralSettings::version() to determine whether | ||||
29 | // an updated version of Dolphin is running. | ||||
30 | const int CurrentDolphinVersion = 200; | ||||
31 | } | ||||
32 | | ||||
33 | void DolphinSingleWindow::open(QList<QUrl> urls) { | ||||
34 | QLocalSocket socket; | ||||
35 | socket.connectToServer(SERVER_NAME); | ||||
36 | qDebug() << "Trying to connect to server"; | ||||
37 | int result = socket.waitForConnected(TIME_OUT); | ||||
38 | qDebug() << result; | ||||
39 | if (result) { | ||||
40 | qDebug() << "Connected To Server"; | ||||
41 | // send a message to the server | ||||
42 | QByteArray block; | ||||
43 | QDataStream out(&block, QIODevice::WriteOnly); | ||||
44 | out.setVersion(QDataStream::Qt_4_7); | ||||
45 | out << urls; | ||||
46 | out.device()->seek(0); | ||||
47 | socket.write(block); | ||||
48 | socket.flush(); | ||||
49 | qDebug() << "Client to Server : Open New Tab"; | ||||
50 | qDebug() << urls; | ||||
51 | qDebug() << "Quitting"; | ||||
52 | return; | ||||
53 | } else { | ||||
54 | DolphinSingleWindow *window = getWindow(); | ||||
55 | window->openDirectories(urls, false); | ||||
56 | window->show(); | ||||
57 | window->createNewServer(); | ||||
58 | //Restore window if minimized | ||||
59 | window->showNormal(); | ||||
60 | } | ||||
61 | } | ||||
62 | | ||||
63 | DolphinSingleWindow *DolphinSingleWindow::getWindow() { | ||||
64 | if (!window) { | ||||
65 | qDebug() << "Creating the Window"; | ||||
66 | window = new DolphinSingleWindow(); | ||||
67 | } | ||||
68 | return window; | ||||
69 | } | ||||
70 | DolphinSingleWindow::DolphinSingleWindow() { createNewServer(); } | ||||
71 | | ||||
72 | DolphinSingleWindow::~DolphinSingleWindow() {} | ||||
73 | | ||||
74 | void DolphinSingleWindow::hookLocalConnection() { | ||||
75 | qDebug() << "Hook"; | ||||
76 | QLocalSocket *socket = server->nextPendingConnection(); | ||||
77 | if (socket) { | ||||
78 | socket->waitForReadyRead(2 * TIME_OUT); | ||||
79 | QList<QUrl> message; | ||||
80 | QDataStream in(socket); | ||||
81 | in >> message; | ||||
82 | qDebug() << "Server Received new Message"; | ||||
83 | qDebug() << message; | ||||
84 | delete socket; | ||||
85 | QList<QUrl> urls; | ||||
86 | if (urls.isEmpty()) { | ||||
87 | // We need at least one URL to open Dolphin | ||||
88 | urls.append(Dolphin::homeUrl()); | ||||
89 | } | ||||
90 | | ||||
91 | window->openDirectories(message, false); | ||||
92 | //Restore window if minimized | ||||
93 | window->showNormal(); | ||||
94 | } | ||||
95 | } | ||||
96 | void DolphinSingleWindow::createNewServer() { | ||||
97 | server = new QLocalServer(this); | ||||
98 | connect(server, SIGNAL(newConnection()), this, SLOT(hookLocalConnection())); | ||||
99 | if (!server->listen(SERVER_NAME)) { | ||||
100 | if (server->serverError() == QAbstractSocket::AddressInUseError) { | ||||
101 | qDebug() << "Server Address is in use, Removing it =D"; | ||||
102 | QLocalServer::removeServer(SERVER_NAME); // <-- A key | ||||
103 | server->listen(SERVER_NAME); // Listen again | ||||
104 | } | ||||
105 | } | ||||
106 | qDebug() << "Server Address is Set"; | ||||
107 | } |