Beispiel #1
0
static inline CcnetMessage *
create_message (SeafMqManager *mgr, const char *app, const char *body, int flags)
{
    CcnetClient *client = mgr->seaf->session;
    CcnetMessage *msg;
    
    char *from = client->base.id;
    char *to = client->base.id;

    msg = ccnet_message_new (from, to, app, body, flags);
    return msg;
}
Beispiel #2
0
static void notify_found_peer (CcnetSession *session, CcnetPeer *peer)
{
    char buf[128];

    sprintf (buf, "found %s\n", peer->id);
    CcnetMessage *message = ccnet_message_new (session->base.id,
                                               session->base.id,
                                               "System", buf, 0);
    ccnet_message_manager_add_msg (session->msg_mgr,
                                   message, MSG_TYPE_SYS);
    
}
void OpenLocalHelper::sendOpenLocalFileMessage(const char *url)
{
    QString content = kOpenLocalFilePrefix;
    content += QString::fromUtf8(url);

    CcnetMessage *open_local_msg;
    open_local_msg = ccnet_message_new (sync_client_->base.id,
                                      sync_client_->base.id,
                                      kAppletCommandsMQ, content.toUtf8().data(), 0);

    ccnet_client_send_message(sync_client_, open_local_msg);

    ccnet_message_free(open_local_msg);
    g_object_unref (sync_client_);
}
void do_stop()
{
    CcnetClient *sync_client = ccnet_client_new();
    const QString ccnet_dir = defaultCcnetDir();
    if (ccnet_client_load_confdir(sync_client, NULL, toCStr(ccnet_dir)) <  0) {
        return;
    }

    if (ccnet_client_connect_daemon(sync_client, CCNET_CLIENT_SYNC) < 0) {
        return;
    }

    CcnetMessage *quit_message;
    quit_message = ccnet_message_new (sync_client->base.id,
                                      sync_client->base.id,
                                      kAppletCommandsMQ, "quit", 0);

    ccnet_client_send_message(sync_client, quit_message);

    ccnet_message_free(quit_message);
    g_object_unref (sync_client);
}
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
        }
    }
}
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
        }
    }
}