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 <KOverlayIconPlugin>
21 #include <KPluginFactory>
22 #include <QtNetwork/QLocalSocket>
23 #include <KIOCore/kfileitem.h>
24 #include <QDir>
25 #include <QTimer>
26 #include "ownclouddolphinpluginhelper.h"
27 
28 class OwncloudDolphinPlugin : public KOverlayIconPlugin
29 {
30     Q_PLUGIN_METADATA(IID "com.owncloud.ovarlayiconplugin" FILE "ownclouddolphinoverlayplugin.json")
31     Q_OBJECT
32 
33     using StatusMap = QHash<QByteArray, QByteArray>;
34     StatusMap m_status;
35 
36 public:
37 
OwncloudDolphinPlugin()38     OwncloudDolphinPlugin() {
39         auto helper = OwncloudDolphinPluginHelper::instance();
40         QObject::connect(helper, &OwncloudDolphinPluginHelper::commandRecieved,
41                          this, &OwncloudDolphinPlugin::slotCommandRecieved);
42     }
43 
getOverlays(const QUrl & url)44     QStringList getOverlays(const QUrl& url) override {
45         auto helper = OwncloudDolphinPluginHelper::instance();
46         if (!helper->isConnected())
47             return QStringList();
48         if (!url.isLocalFile())
49             return QStringList();
50         QDir localPath(url.toLocalFile());
51         const QByteArray localFile = localPath.canonicalPath().toUtf8();
52 
53         helper->sendCommand(QByteArray("RETRIEVE_FILE_STATUS:" + localFile + "\n"));
54 
55         StatusMap::iterator it = m_status.find(localFile);
56         if (it != m_status.constEnd()) {
57             return  overlaysForString(*it);
58         }
59         return QStringList();
60     }
61 
62 private:
overlaysForString(const QByteArray & status)63     QStringList overlaysForString(const QByteArray &status) {
64         QStringList r;
65         if (status.startsWith("NOP"))
66             return r;
67 
68         if (status.startsWith("OK"))
69             r << "vcs-normal";
70         if (status.startsWith("SYNC") || status.startsWith("NEW"))
71             r << "vcs-update-required";
72         if (status.startsWith("IGNORE") || status.startsWith("WARN"))
73             r << "vcs-locally-modified-unstaged";
74         if (status.startsWith("ERROR"))
75             r << "vcs-conflicting";
76 
77         if (status.contains("+SWM"))
78             r << "document-share";
79 
80         return r;
81     }
82 
slotCommandRecieved(const QByteArray & line)83     void slotCommandRecieved(const QByteArray &line) {
84 
85         QList<QByteArray> tokens = line.split(':');
86         if (tokens.count() < 3)
87             return;
88         if (tokens[0] != "STATUS" && tokens[0] != "BROADCAST")
89             return;
90         if (tokens[2].isEmpty())
91             return;
92 
93         // We can't use tokens[2] because the filename might contain ':'
94         int secondColon = line.indexOf(":", line.indexOf(":") + 1);
95         const QByteArray name = line.mid(secondColon + 1);
96         QByteArray &status = m_status[name]; // reference to the item in the hash
97         if (status == tokens[1])
98             return;
99         status = tokens[1];
100 
101         emit overlaysChanged(QUrl::fromLocalFile(QString::fromUtf8(name)), overlaysForString(status));
102     }
103 };
104 
105 #include "ownclouddolphinoverlayplugin.moc"
106