void MessageListener::handleMessage(CcnetMessage *message)
{
    // qWarning("got a message: %s %s.", message->app, message->body);

    char *type = NULL;
    char *content = NULL;

    if (IS_APP_MSG(message, kAppletCommandsMQ)) {
        if (g_strcmp0(message->body, "quit") == 0) {
            qWarning("[Message Listener] Got a quit command. Quit now.");
            QCoreApplication::exit(0);
            return;
        }
        if (g_strcmp0(message->body, "syn_activate") == 0) {
            qWarning("[Message Listener] Got an activate command.");
            CcnetMessage *ack_message;
            ack_message = ccnet_message_new(async_client_->base.id,
                                            async_client_->base.id,
                                            kAppletCommandsMQ, "ack_activate", 0);
            ccnet_mqclient_proc_put_message(mqclient_proc_, ack_message);
            ccnet_message_free(ack_message);

            seafApplet->mainWindow()->showWindow();
        }

        const char *kOpenLocalFilePrefix = "open-local-file\t";
        if (strstr(message->body, kOpenLocalFilePrefix) == message->body) {
            OpenLocalHelper::instance()->openLocalFile(QUrl::fromEncoded(message->body + strlen(kOpenLocalFilePrefix)));
        }

    } else if (IS_APP_MSG(message, kSeafileNotificationsMQ)) {
        if (parse_seafile_notification (message->body, &type, &content) < 0)
            return;

        if (strcmp(type, "transfer") == 0) {
            // empty
        } else if (strcmp(type, "repo.deleted_on_relay") == 0) {
            QString buf = tr("\"%1\" is unsynced. \nReason: Deleted on server").arg(QString::fromUtf8(content));
            seafApplet->trayIcon()->showMessage(getBrand(), buf);
        } else if (strcmp(type, "sync.done") == 0) {
            /* format: a concatenation of (repo_name, repo_id, commmit_id,
             * previous_commit_id, description), separated by tabs */
            QStringList slist = QString::fromUtf8(content).split("\t");
            if (slist.count() != 5) {
                qWarning("Bad sync.done message format");
                return;
            }

            QString title = tr("\"%1\" is synchronized").arg(slist.at(0));
            QString repo_id = slist.at(1).trimmed();
            QString commit_id = slist.at(2).trimmed();
            QString previous_commit_id = slist.at(3).trimmed();
            QString desc = slist.at(4).trimmed();

            seafApplet->trayIcon()->showMessage(title, translateCommitDesc(desc), repo_id, commit_id, previous_commit_id);

        } else if (strcmp(type, "sync.conflict") == 0) {
            json_error_t error;
            json_t *object = json_loads(content, 0, &error);
            if (!object) {
                qWarning("Failed to parse json: %s", error.text);
                return;
            }

            QString repo_id = QString::fromUtf8(json_string_value(json_object_get(object, "repo_id")));
            QString title = QString::fromUtf8(json_string_value(json_object_get(object, "repo_name")));
            QString path = QString::fromUtf8(json_string_value(json_object_get(object, "path")));
            QString msg = tr("File %1 conflict").arg(path);

            seafApplet->trayIcon()->showMessage(title, msg, repo_id);

            json_decref(object);
        } else if (strcmp(type, "sync.error") == 0) {
            json_error_t error;
            json_t *object = json_loads(content, 0, &error);
            if (!object) {
                qWarning("Failed to parse json: %s", error.text);
                return;
            }

            QString repo_id = QString::fromUtf8(json_string_value(json_object_get(object, "repo_id")));
            QString title = QString::fromUtf8(json_string_value(json_object_get(object, "repo_name")));
            QString path = QString::fromUtf8(json_string_value(json_object_get(object, "path")));
            int err_id = json_integer_value(json_object_get(object, "err_id"));
            QString msg;
            switch (err_id) {
            case SYNC_ERROR_ID_FILE_LOCKED_BY_APP:
                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);
                break;
            case SYNC_ERROR_ID_FOLDER_LOCKED_BY_APP:
                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);
                break;
            case SYNC_ERROR_ID_FILE_LOCKED:
                msg = tr("Failed to sync file %1\nFile is locked by other user on the server. Update to this file is not uploaded.").arg(path);
                break;
            case SYNC_ERROR_ID_INVALID_PATH:
                msg = tr("Failed to sync %1\nFile path contains invalid characters. It is not synced to this computer.").arg(path);
                break;
            case SYNC_ERROR_ID_INDEX_ERROR:
                msg = tr("Failed to index file %1\nPlease check file permission and disk space.").arg(path);
                break;
            default:
                qWarning("Unknown sync error id %d", err_id);
                json_decref(object);
                return;
            }

            seafApplet->trayIcon()->showMessage(title, msg, repo_id);

            json_decref(object);
        } else if (strcmp(type, "sync.access_denied") == 0) {
            /* format: <repo_name\trepo_id> */
            QStringList slist = QString::fromUtf8(content).split("\t");
            if (slist.count() != 2) {
                qWarning("Bad sync.access_denied message format");
                return;
            }
            QString buf = tr("\"%1\" failed to sync. \nAccess denied to service").arg(slist.at(0));
            seafApplet->trayIcon()->showMessage(getBrand(), buf);

        } else if (strcmp(type, "sync.quota_full") == 0) {
            /* format: <repo_name\trepo_id> */
            QStringList slist = QString::fromUtf8(content).split("\t");
            if (slist.count() != 2) {
                qWarning("Bad sync.quota_full message format");
                return;
            }

            QString buf = tr("\"%1\" failed to sync.\nThe library owner's storage space is used up.").arg(slist.at(0));
            seafApplet->trayIcon()->showMessage(getBrand(), buf);
#if defined(Q_OS_MAC)
        } else if (strcmp(type, "repo.setwktree") == 0) {
            //seafile_set_repofolder_icns (content);
        } else if (strcmp(type, "repo.unsetwktree") == 0) {
            //seafile_unset_repofolder_icns (content);
#endif
        }
    }
}
Ejemplo n.º 2
0
/* Wrap around ccnet_message_new since all messages we use are local. */
static inline void
_send_message (SeafMqManager *mgr, CcnetMessage *msg)
{
    CcnetMqclientProc *mqclient_proc = mgr->priv->mqclient_proc;
    ccnet_mqclient_proc_put_message (mqclient_proc, msg);
}
void MessageListener::handleMessage(CcnetMessage *message)
{
    // qWarning("got a message: %s %s.", message->app, message->body);

    char *type = NULL;
    char *content = NULL;

    if (IS_APP_MSG(message, kAppletCommandsMQ)) {
        if (g_strcmp0(message->body, "quit") == 0) {
            qWarning("[Message Listener] Got a quit command. Quit now.");
            QCoreApplication::exit(0);
            return;
        }
        if (g_strcmp0(message->body, "syn_activate") == 0) {
            qWarning("[Message Listener] Got an activate command.");
            CcnetMessage *ack_message;
            ack_message = ccnet_message_new(async_client_->base.id,
                                            async_client_->base.id,
                                            kAppletCommandsMQ, "ack_activate", 0);
            ccnet_mqclient_proc_put_message(mqclient_proc_, ack_message);
            ccnet_message_free(ack_message);

            seafApplet->mainWindow()->showWindow();
        }

        const char *kOpenLocalFilePrefix = "open-local-file\t";
        if (strstr(message->body, kOpenLocalFilePrefix) == message->body) {
            OpenLocalHelper::instance()->openLocalFile(QUrl::fromEncoded(message->body + strlen(kOpenLocalFilePrefix)));
        }

    } else if (IS_APP_MSG(message, kSeafileNotificationsMQ)) {
        if (parse_seafile_notification (message->body, &type, &content) < 0)
            return;

        if (strcmp(type, "transfer") == 0) {
            // empty
        } else if (strcmp(type, "repo.deleted_on_relay") == 0) {
            QString buf = tr("\"%1\" is unsynced. \nReason: Deleted on server").arg(QString::fromUtf8(content));
            seafApplet->trayIcon()->showMessage(getBrand(), buf);
        } else if (strcmp(type, "sync.done") == 0) {
            /* format: repo_name \t repo_id \t description */
            QStringList slist = QString::fromUtf8(content).split("\t");
            if (slist.count() != 3) {
                qWarning("Bad sync.done message format");
                return;
            }

            QString title = tr("\"%1\" is synchronized").arg(slist.at(0));
            QString repo_id = slist.at(1).trimmed();
            QString buf = slist.at(2).trimmed();

            seafApplet->trayIcon()->showMessageWithRepo(repo_id, title, translateCommitDesc(buf));

        } else if (strcmp(type, "sync.access_denied") == 0) {
            /* format: <repo_name\trepo_id> */
            QStringList slist = QString::fromUtf8(content).split("\t");
            if (slist.count() != 2) {
                qWarning("Bad sync.access_denied message format");
                return;
            }
            QString buf = tr("\"%1\" failed to sync. \nAccess denied to service").arg(slist.at(0));
            seafApplet->trayIcon()->showMessage(getBrand(), buf);

        } else if (strcmp(type, "sync.quota_full") == 0) {
            /* format: <repo_name\trepo_id> */
            QStringList slist = QString::fromUtf8(content).split("\t");
            if (slist.count() != 2) {
                qWarning("Bad sync.quota_full message format");
                return;
            }

            QString buf = tr("\"%1\" failed to sync.\nThe library owner's storage space is used up.").arg(slist.at(0));
            seafApplet->trayIcon()->showMessage(getBrand(), buf);
#if defined(Q_OS_MAC)
        } else if (strcmp(type, "repo.setwktree") == 0) {
            //seafile_set_repofolder_icns (content);
        } else if (strcmp(type, "repo.unsetwktree") == 0) {
            //seafile_unset_repofolder_icns (content);
#endif
        }
    }
}