1/*
2 * Copyright (C) by Dominik Schmidt <dev@dominik-schmidt.de>
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, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15
16#ifndef SOCKETAPI_H
17#define SOCKETAPI_H
18
19#include "syncfileitem.h"
20#include "syncfilestatus.h"
21#include "sharedialog.h" // for the ShareDialogStartPage
22#include "common/syncjournalfilerecord.h"
23
24#if defined(Q_OS_MAC)
25#include "socketapisocket_mac.h"
26#else
27#include <QLocalServer>
28typedef QLocalServer SocketApiServer;
29#endif
30
31class QUrl;
32class QLocalSocket;
33class QStringList;
34
35namespace OCC {
36
37class SyncFileStatus;
38class Folder;
39class SocketListener;
40
41/**
42 * @brief The SocketApi class
43 * @ingroup gui
44 */
45class SocketApi : public QObject
46{
47 Q_OBJECT
48
49public:
50 explicit SocketApi(QObject *parent = 0);
51 virtual ~SocketApi();
52
53public slots:
54 void slotUpdateFolderView(Folder *f);
55 void slotUnregisterPath(const QString &alias);
56 void slotRegisterPath(const QString &alias);
57 void broadcastStatusPushMessage(const QString &systemPath, SyncFileStatus fileStatus);
58
59signals:
60 void shareCommandReceived(const QString &sharePath, const QString &localPath, ShareDialogStartPage startPage);
61
62private slots:
63 void slotNewConnection();
64 void onLostConnection();
65 void slotSocketDestroyed(QObject *obj);
66 void slotReadSocket();
67
68 static void copyUrlToClipboard(const QString &link);
69 static void emailPrivateLink(const QString &link);
70 static void openPrivateLink(const QString &link);
71
72private:
73 // Helper structure for getting information on a file
74 // based on its local path - used for nearly all remote
75 // actions.
76 struct FileData
77 {
78 static FileData get(const QString &localFile);
79 SyncFileStatus syncFileStatus() const;
80 SyncJournalFileRecord journalRecord() const;
81
82 Folder *folder;
83 // Absolute path of the file locally. (May be a virtual file)
84 QString localPath;
85 // Relative path of the file locally, as in the DB. (May be a virtual file)
86 QString folderRelativePath;
87 // Path of the file on the server (In case of virtual file, it points to the actual file)
88 QString serverRelativePath;
89 };
90
91 void broadcastMessage(const QString &msg, bool doWait = false);
92
93 // opens share dialog, sends reply
94 void processShareRequest(const QString &localFile, SocketListener *listener, ShareDialogStartPage startPage);
95
96 Q_INVOKABLE void command_RETRIEVE_FOLDER_STATUS(const QString &argument, SocketListener *listener);
97 Q_INVOKABLE void command_RETRIEVE_FILE_STATUS(const QString &argument, SocketListener *listener);
98
99 Q_INVOKABLE void command_VERSION(const QString &argument, SocketListener *listener);
100
101 Q_INVOKABLE void command_SHARE_MENU_TITLE(const QString &argument, SocketListener *listener);
102
103 // The context menu actions
104 Q_INVOKABLE void command_SHARE(const QString &localFile, SocketListener *listener);
105 Q_INVOKABLE void command_MANAGE_PUBLIC_LINKS(const QString &localFile, SocketListener *listener);
106 Q_INVOKABLE void command_COPY_PUBLIC_LINK(const QString &localFile, SocketListener *listener);
107 Q_INVOKABLE void command_COPY_PRIVATE_LINK(const QString &localFile, SocketListener *listener);
108 Q_INVOKABLE void command_EMAIL_PRIVATE_LINK(const QString &localFile, SocketListener *listener);
109 Q_INVOKABLE void command_OPEN_PRIVATE_LINK(const QString &localFile, SocketListener *listener);
110 Q_INVOKABLE void command_DOWNLOAD_VIRTUAL_FILE(const QString &filesArg, SocketListener *listener);
111
112 // Fetch the private link and call targetFun
113 void fetchPrivateLinkUrlHelper(const QString &localFile, const std::function<void(const QString &url)> &targetFun);
114
115 /** Sends translated/branded strings that may be useful to the integration */
116 Q_INVOKABLE void command_GET_STRINGS(const QString &argument, SocketListener *listener);
117
118 // Sends the context menu options relating to sharing to listener
119 void sendSharingContextMenuOptions(const FileData &fileData, SocketListener *listener);
120
121 /** Send the list of menu item. (added in version 1.1)
122 * argument is a list of files for which the menu should be shown, separated by '\x1e'
123 * Reply with GET_MENU_ITEMS:BEGIN
124 * followed by several MENU_ITEM:[Action]:[flag]:[Text]
125 * If flag contains 'd', the menu should be disabled
126 * and ends with GET_MENU_ITEMS:END
127 */
128 Q_INVOKABLE void command_GET_MENU_ITEMS(const QString &argument, SocketListener *listener);
129
130 QString buildRegisterPathMessage(const QString &path);
131
132 QSet<QString> _registeredAliases;
133 QList<SocketListener> _listeners;
134 SocketApiServer _localServer;
135};
136}
137#endif // SOCKETAPI_H
138