Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unisonfolder.cpp « mirall « src - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e4eb56d33b6ae7d20165764f769fdbdd2d72628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) by Duncan Mac-Vicar P. <duncan@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */

#include "mirall/unisonfolder.h"

#include <QDebug>
#include <QDir>
#include <QMutexLocker>
#include <QStringList>
#include <QTextStream>

namespace Mirall {

    UnisonFolder::UnisonFolder(const QString &alias,
                               const QString &path,
                               const QString &secondPath,
                               QObject *parent)
      : Folder(alias, path, secondPath, parent),
      _unison(new QProcess(this)),
      _syncCount(0)
{
    QObject::connect(_unison, SIGNAL(readyReadStandardOutput()),
                     SLOT(slotReadyReadStandardOutput()));

    QObject::connect(_unison, SIGNAL(readyReadStandardError()),
                     SLOT(slotReadyReadStandardError()));

    QObject::connect(_unison, SIGNAL(stateChanged(QProcess::ProcessState)),
                     SLOT(slotStateChanged(QProcess::ProcessState)));

    QObject::connect(_unison, SIGNAL(error(QProcess::ProcessError)),
                     SLOT(slotError(QProcess::ProcessError)));

    QObject::connect(_unison, SIGNAL(started()),
                     SLOT(slotStarted()));

    QObject::connect(_unison, SIGNAL(finished(int, QProcess::ExitStatus)),
                     SLOT(slotFinished(int, QProcess::ExitStatus)));
}

UnisonFolder::~UnisonFolder()
{
}

bool UnisonFolder::isBusy() const
{
    return (_unison->state() != QProcess::NotRunning);
}

void UnisonFolder::startSync(const QStringList &pathList)
{
    QMutexLocker locker(&_syncMutex);

    emit syncStarted();

    QString program = "unison";
    QStringList args;
    args << "-ui" << "text";
    args << "-auto" << "-batch";

    args << "-confirmbigdel=false";

    // only use -path in after a full synchronization
    // already happened, which we do only on the first
    // sync when the program is started
    if (_syncCount > 0 ) {
        // may be we should use a QDir in the API itself?
        QDir root(path());
        foreach( const QString& changedPath, pathList) {
            args << "-path" << root.relativeFilePath(changedPath);
        }
    }

    args  << path();
    args  << secondPath();

    qDebug() << "    * Unison: will use" << pathList.size() << "path arguments";
    _unison->start(program, args);
}

void UnisonFolder::slotTerminateSync()
{
    if( _unison )
        _unison->terminate();
}

void UnisonFolder::slotStarted()
{
    qDebug() << "    * Unison process started ( PID " << _unison->pid() << ")";
    _syncCount++;

    //qDebug() << _unison->readAllStandardOutput();;
}

void UnisonFolder::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    qDebug() << "    * Unison process finished with status" << exitCode;

    //if (exitCode != 0) {
    qDebug() << _lastOutput;
        //}

    // parse a summary from here:
    //[BGN] Copying zw.png from //piscola//space/store/folder1 to /space/mirall/folder1
    //[BGN] Deleting gn.png from /space/mirall/folder1
    //[END] Deleting gn.png

    // from stderr:
    //Reconciling changes
    //         <---- new file   Package.h

    _lastOutput.clear();

    emit syncFinished((exitCode != 0) ?
                      SyncResult(SyncResult::Error)
                      : SyncResult(SyncResult::Success));
}

void UnisonFolder::slotReadyReadStandardOutput()
{
    QTextStream stream(&_lastOutput);
    stream << _unison->readAllStandardOutput();;
}

void UnisonFolder::slotReadyReadStandardError()
{
    QTextStream stream(&_lastOutput);
    stream << _unison->readAllStandardError();;
}

void UnisonFolder::slotStateChanged(QProcess::ProcessState state)
{
    //qDebug() << "changed: " << state;
}

void UnisonFolder::slotError(QProcess::ProcessError error)
{
    //qDebug() << "error: " << error;
}

} // ns