"Fossies" - the Fresh Open Source Software Archive 
Member "seafile-client-7.0.4/src/message-poller.cpp" (19 Nov 2019, 7042 Bytes) of package /linux/www/seafile-client-7.0.4.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "message-poller.cpp" see the
Fossies "Dox" file reference documentation and the last
Fossies "Diffs" side-by-side code changes report:
7.0.2_vs_7.0.3.
1 #include <QTimer>
2 #include <QDateTime>
3
4 #include "utils/utils.h"
5 #include "utils/translate-commit-desc.h"
6 #include "utils/json-utils.h"
7 #include "utils/file-utils.h"
8 #include "seafile-applet.h"
9 #include "daemon-mgr.h"
10 #include "settings-mgr.h"
11 #include "rpc/rpc-client.h"
12 #include "rpc/sync-error.h"
13 #include "ui/tray-icon.h"
14
15 #include "message-poller.h"
16 #include <seafile/seafile-error.h>
17
18 namespace {
19
20 const int kCheckNotificationIntervalMSecs = 1000;
21
22 } // namespace
23
24 struct SyncNotification {
25 QString type;
26 QString content;
27
28 static SyncNotification fromJson(const json_t* json);
29 };
30
31
32 MessagePoller::MessagePoller(QObject *parent): QObject(parent)
33 {
34 rpc_client_ = new SeafileRpcClient();
35 check_notification_timer_ = new QTimer(this);
36 connect(check_notification_timer_, SIGNAL(timeout()), this, SLOT(checkNotification()));
37 }
38
39 MessagePoller::~MessagePoller()
40 {
41 delete check_notification_timer_;
42 delete rpc_client_;
43 }
44
45 void MessagePoller::start()
46 {
47 rpc_client_->tryConnectDaemon();
48 check_notification_timer_->start(kCheckNotificationIntervalMSecs);
49 connect(seafApplet->daemonManager(), SIGNAL(daemonDead()), this, SLOT(onDaemonDead()));
50 connect(seafApplet->daemonManager(), SIGNAL(daemonRestarted()), this, SLOT(onDaemonRestarted()));
51 }
52
53 void MessagePoller::onDaemonDead()
54 {
55 qDebug("pausing message poller when daemon is dead");
56 check_notification_timer_->stop();
57 }
58
59 void MessagePoller::onDaemonRestarted()
60 {
61 qDebug("reviving message poller when daemon is restarted");
62 if (rpc_client_) {
63 delete rpc_client_;
64 }
65 rpc_client_ = new SeafileRpcClient();
66 rpc_client_->tryConnectDaemon();
67 check_notification_timer_->start(kCheckNotificationIntervalMSecs);
68 }
69
70 void MessagePoller::checkNotification()
71 {
72 json_t *ret;
73 if (!rpc_client_->getSyncNotification(&ret)) {
74 return;
75 }
76 SyncNotification notification = SyncNotification::fromJson(ret);
77 json_decref(ret);
78
79 processNotification(notification);
80 }
81
82 SyncNotification SyncNotification::fromJson(const json_t *root)
83 {
84 SyncNotification notification;
85 Json json(root);
86
87 // char *s = json_dumps(root, 0);
88 // printf ("[%s] %s\n", QDateTime::currentDateTime().toString().toUtf8().data(), s);
89 // qWarning ("[%s] %s\n", QDateTime::currentDateTime().toString().toUtf8().data(), s);
90 // free (s);
91
92 notification.type = json.getString("type");
93 notification.content = json.getString("content");
94 return notification;
95 }
96
97 void MessagePoller::processNotification(const SyncNotification& notification)
98 {
99 const QString& type = notification.type;
100 const QString& content = notification.content;
101 if (type == "transfer") {
102 // empty
103 } else if (type == "sync.done" ||
104 type == "sync.multipart_upload") {
105 /* format: a concatenation of (repo_name, repo_id, commmit_id,
106 * previous_commit_id, description), separated by tabs */
107 QStringList slist = content.split("\t");
108 if (slist.count() != 5) {
109 qWarning("Bad sync.done message format");
110 return;
111 }
112
113 QString title;
114 if (type == "sync.done")
115 title = tr("\"%1\" is synchronized").arg(slist.at(0));
116 else
117 title = tr("Files uploaded to \"%1\"").arg(slist.at(0));
118 QString repo_id = slist.at(1).trimmed();
119 QString commit_id = slist.at(2).trimmed();
120 QString previous_commit_id = slist.at(3).trimmed();
121 QString desc = slist.at(4).trimmed();
122
123 seafApplet->trayIcon()->showMessage(title, translateCommitDesc(desc), repo_id, commit_id, previous_commit_id);
124
125 } else if (type == "sync.error") {
126 json_error_t error;
127 json_t *object = json_loads(toCStr(content), 0, &error);
128 if (!object) {
129 qWarning("Failed to parse json: %s", error.text);
130 return;
131 }
132
133 QString repo_id = QString::fromUtf8(json_string_value(json_object_get(object, "repo_id")));
134 QString title = QString::fromUtf8(json_string_value(json_object_get(object, "repo_name")));
135 QString path = QString::fromUtf8(json_string_value(json_object_get(object, "path")));
136 int err_id = json_integer_value(json_object_get(object, "err_id"));
137 QString msg;
138 switch (err_id) {
139 case SYNC_ERROR_ID_FILE_LOCKED_BY_APP:
140 msg = tr("Failed to sync file %1\nFile is locked by other application. This file will be updated when you close the application.").arg(path);
141 break;
142 case SYNC_ERROR_ID_FOLDER_LOCKED_BY_APP:
143 msg = tr("Failed to sync folder %1\nSome file in this folder is locked by other application. This folder will be updated when you close the application.").arg(path);
144 break;
145 case SYNC_ERROR_ID_FILE_LOCKED:
146 msg = tr("Failed to sync file %1\nFile is locked by another user. Update to this file is not uploaded.").arg(path);
147 break;
148 case SYNC_ERROR_ID_INVALID_PATH:
149 msg = tr("Failed to sync %1\nFile path contains invalid characters. It is not synced to this computer.").arg(path);
150 break;
151 case SYNC_ERROR_ID_INDEX_ERROR:
152 msg = tr("Failed to index file %1\nPlease check file permission and disk space.").arg(path);
153 break;
154 case SYNC_ERROR_ID_PATH_END_SPACE_PERIOD:
155 msg = tr("Failed to sync %1\nFile path is ended with space or period and cannot be created on Windows.").arg(path);
156 break;
157 case SYNC_ERROR_ID_PATH_INVALID_CHARACTER:
158 msg = tr("Failed to sync %1\nFile path contains invalid characters. It is not synced to this computer.").arg(path);
159 break;
160 case SYNC_ERROR_ID_FOLDER_PERM_DENIED:
161 msg = tr("Update to file %1 is denied by folder permission setting.").arg(path);
162 break;
163 case SYNC_ERROR_ID_PERM_NOT_SYNCABLE:
164 msg = tr("No permission to sync folder %1.").arg(path);
165 break;
166 case SYNC_ERROR_ID_UPDATE_TO_READ_ONLY_REPO:
167 msg = tr("Updates in read-only library %1 will not be uploaded.").arg(path);
168 break;
169 case SYNC_ERROR_ID_CONFLICT:
170 msg = tr("Concurrent updates to file. File %1 is saved as conflict file").arg(path);
171 break;
172 case SYNC_ERROR_ID_REMOVE_UNCOMMITTED_FOLDER:
173 msg = tr("Folder %1 is moved to seafile-recycle-bin folder since it contains not-yet uploaded files.").arg(path);
174 break;
175 default:
176 qWarning("Unknown sync error id %d", err_id);
177 json_decref(object);
178 return;
179 }
180
181 seafApplet->trayIcon()->showMessage(title, msg, repo_id);
182
183 json_decref(object);
184
185 } else if (type == "repo.remove") {
186 /* format : <repo_name> */
187 QString repo_name = content;
188 QString buf = tr("Folder for library %1 is removed or moved. The library is unsynced.").arg(repo_name);
189 seafApplet->trayIcon()->showMessage(getBrand(), buf);
190 }
191 }