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;
            // TODO we may be blocked, any improvement?
            // e.g. start a new thread to send ack?
            ack_message = ccnet_message_new(sync_client_->base.id,
                                            sync_client_->base.id,
                                            kAppletCommandsMQ, "ack_activate", 0);
            ccnet_client_send_message(sync_client_, 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
        }
    }
}