1 /******************************************************************************
2  *   Copyright (C) 2014 by Olivier Goffart <ogoffart@woboq.com                *
3  *                                                                            *
4  *   This program is free software; you can redistribute it and/or modify     *
5  *   it under the terms of the GNU General Public License as published by     *
6  *   the Free Software Foundation; either version 2 of the License, or        *
7  *   (at your option) any later version.                                      *
8  *                                                                            *
9  *   This program is distributed in the hope that it will be useful,          *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
12  *   GNU General Public License for more details.                             *
13  *                                                                            *
14  *   You should have received a copy of the GNU General Public License        *
15  *   along with this program; if not, write to the                            *
16  *   Free Software Foundation, Inc.,                                          *
17  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA               *
18  ******************************************************************************/
19 
20 #include <QtNetwork/QLocalSocket>
21 #include <qcoreevent.h>
22 #include <QStandardPaths>
23 #include <QFile>
24 #include "ownclouddolphinpluginhelper.h"
25 
instance()26 OwncloudDolphinPluginHelper* OwncloudDolphinPluginHelper::instance()
27 {
28     static OwncloudDolphinPluginHelper self;
29     return &self;
30 }
31 
OwncloudDolphinPluginHelper()32 OwncloudDolphinPluginHelper::OwncloudDolphinPluginHelper()
33 {
34     connect(&_socket, &QLocalSocket::connected, this, &OwncloudDolphinPluginHelper::slotConnected);
35     connect(&_socket, &QLocalSocket::readyRead, this, &OwncloudDolphinPluginHelper::slotReadyRead);
36     _connectTimer.start(45 * 1000, Qt::VeryCoarseTimer, this);
37     tryConnect();
38 }
39 
timerEvent(QTimerEvent * e)40 void OwncloudDolphinPluginHelper::timerEvent(QTimerEvent *e)
41 {
42     if (e->timerId() == _connectTimer.timerId()) {
43         tryConnect();
44         return;
45     }
46     QObject::timerEvent(e);
47 }
48 
isConnected() const49 bool OwncloudDolphinPluginHelper::isConnected() const
50 {
51     return _socket.state() == QLocalSocket::ConnectedState;
52 }
53 
sendCommand(const char * data)54 void OwncloudDolphinPluginHelper::sendCommand(const char* data)
55 {
56     _socket.write(data);
57     _socket.flush();
58 }
59 
slotConnected()60 void OwncloudDolphinPluginHelper::slotConnected()
61 {
62     sendCommand("VERSION:\n");
63     sendCommand("GET_STRINGS:\n");
64 }
65 
tryConnect()66 void OwncloudDolphinPluginHelper::tryConnect()
67 {
68     if (_socket.state() != QLocalSocket::UnconnectedState) {
69         return;
70     }
71 
72     QString socketPath = QStandardPaths::locate(QStandardPaths::RuntimeLocation,
73                                                 APPLICATION_SHORTNAME,
74                                                 QStandardPaths::LocateDirectory);
75     if(socketPath.isEmpty())
76         return;
77 
78     _socket.connectToServer(socketPath + QLatin1String("/socket"));
79 }
80 
slotReadyRead()81 void OwncloudDolphinPluginHelper::slotReadyRead()
82 {
83     while (_socket.bytesAvailable()) {
84         _line += _socket.readLine();
85         if (!_line.endsWith("\n"))
86             continue;
87         QByteArray line;
88         qSwap(line, _line);
89         line.chop(1);
90         if (line.isEmpty())
91             continue;
92 
93         if (line.startsWith("REGISTER_PATH:")) {
94             auto col = line.indexOf(':');
95             QString file = QString::fromUtf8(line.constData() + col + 1, line.size() - col - 1);
96             _paths.append(file);
97             continue;
98         } else if (line.startsWith("STRING:")) {
99             auto args = QString::fromUtf8(line).split(QLatin1Char(':'));
100             if (args.size() >= 3) {
101                 _strings[args[1]] = args.mid(2).join(QLatin1Char(':'));
102             }
103             continue;
104         } else if (line.startsWith("VERSION:")) {
105             auto args = line.split(':');
106             auto version = args.value(2);
107             _version = version;
108             if (!version.startsWith("1.")) {
109                 // Incompatible version, disconnect forever
110                 _connectTimer.stop();
111                 _socket.disconnectFromServer();
112                 return;
113             }
114         }
115         emit commandRecieved(line);
116     }
117 }
118