File: webdavfolderlistmodel.cpp

package info (click to toggle)
lomiri-cloudsync-app 0.8.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,560 kB
  • sloc: cpp: 1,053; python: 67; javascript: 25; makefile: 21; sh: 9
file content (233 lines) | stat: -rw-r--r-- 5,637 bytes parent folder | download | duplicates (2)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "webdavfolderlistmodel.h"

#include <QDebug>

webdavfolderlistmodel::webdavfolderlistmodel(QObject *parent) : QAbstractListModel(parent)
{
    connect(this, SIGNAL(credentialsChanged()), this, SLOT(setWebdavCredentials()));

    connect(&m_parser, SIGNAL(finished()), this, SLOT(loadFolderList()));
    connect(&m_parser, SIGNAL(errorChanged(QString)), this, SLOT(printError(QString)));
    connect(&m_webdav, SIGNAL(errorChanged(QString)), this, SLOT(printError(QString)));

}

void webdavfolderlistmodel::loadFolderList()
{
    m_folderList = m_parser.getList();

    QMutableListIterator<QWebdavItem> i(m_folderList);
    while (i.hasNext()) {
        QWebdavItem item = i.next();

        if (!m_showFiles){
            if (!item.isDir())
                i.remove();
        }

       if(!m_showDirs){
            if (item.isDir())
                i.remove();
        }
    }

    emit endResetModel();
    emit countChanged();
}

void webdavfolderlistmodel::setWebdavCredentials()
{
    if(!m_username.isNull() && !m_password.isNull() && !m_serverUrl.isNull()){
        qDebug() << "webdavfolderlistmodel::setWebdavCredentials" << "serverUrl:" << m_serverUrl;

        // Target Credentials
        //serverUrl = https://myserver.com/owncloud
        //url = myserver.com
        //path = /owncloud/remote.php/webdav/

        QUrl sUrl(m_serverUrl);

        QString protocol = sUrl.scheme();
        qDebug() << "webdav::setConnectionSettings" << "protocol:" << protocol;

        QString url = sUrl.host();
        qDebug() << "webdav::setConnectionSettings" << "url:" << url;

        int port = sUrl.port();
        if(port == -1) port = 0;
        qDebug() << "webdav::setConnectionSettings" << "port:" << port;

        QString path = sUrl.path();
        path.append("/remote.php/webdav/");
        qDebug() << "webdav::setConnectionSettings" << "path:" << path;

        QWebdav::QWebdavConnectionType connectionType;

        if (protocol == "https")
        {
            connectionType = QWebdav::HTTPS;
        }else{
            connectionType = QWebdav::HTTP;
        }

        //m_webdav.setConnectionSettings(connectionType, url, path, m_username, m_password);
        m_webdav.setConnectionSettings(connectionType, url, path, m_username, m_password, port);
        // qDebug() << "webdav::setConnectionSettings" << "username:" << m_username;
        // qDebug() << "webdav::setConnectionSettings" << "password:" << m_password;
    }
}

QHash<int, QByteArray> webdavfolderlistmodel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[FileNameRole] = "fileName";
    roles[FilePathRole] = "filePath";
    return roles;
}

void webdavfolderlistmodel::getFolderList()
{
    qDebug() << "webdavfolderlistmodel::getFolderList";
    m_parser.listDirectory(&m_webdav, m_path);
}

int webdavfolderlistmodel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_folderList.length();
}

QString webdavfolderlistmodel::folder(){

    return m_folder;
}

QString webdavfolderlistmodel::getErrorMsg(){

    return m_errorMsg;
}

void webdavfolderlistmodel::setFolder(QString &folder)
{
    qDebug() << "webdavfolderlistmodel::setFolder - folder changed:" << folder ;
    m_folder = folder;
    //m_webdav.setPath(folder);

    if(!folder.endsWith("/"))
    {
        folder.append("/");
    }
    m_path = folder;

    getFolderList();

    emit folderChanged();
}

void webdavfolderlistmodel::newWebDavFolder(QString folderPath)
{
    m_webdav.mkdir(folderPath);
    getFolderList();

    emit folderChanged();
}

void webdavfolderlistmodel::setUsername(QString &username)
{
    m_username = username;
    emit credentialsChanged();
}

void webdavfolderlistmodel::setPassword(QString &password)
{
    m_password = password;
    emit credentialsChanged();
}

void webdavfolderlistmodel::setServerUrl(QString &serverUrl)
{
    m_serverUrl = serverUrl;
    emit credentialsChanged();
}

bool webdavfolderlistmodel::showDirs()
{
    return m_showDirs;
}

void webdavfolderlistmodel::setShowDirs(bool &showDirs)
{
    m_showDirs = showDirs;
}

bool webdavfolderlistmodel::showFiles()
{
    return m_showFiles;
}

void webdavfolderlistmodel::setShowFiles(bool &showFiles)
{
    m_showFiles = showFiles;
}

bool webdavfolderlistmodel::showHidden()
{
    return m_showHidden;
}

void webdavfolderlistmodel::setShowHidden(bool &showHidden)
{
    m_showHidden = showHidden;
}

QVariant webdavfolderlistmodel::data(const QModelIndex &index, int role) const
{
    QVariant modeldata;
    if (role == FileNameRole)
    {
        modeldata = getName(index.row());
    }
    else if (role == FilePathRole)
    {
        modeldata = getPath(index.row());
    }

    return modeldata;
}

QString webdavfolderlistmodel::getName(int index) const
{
    return m_folderList.at(index).name();
}

QString webdavfolderlistmodel::getPath(int index) const
{
    return m_folderList.at(index).path();
}

bool webdavfolderlistmodel::isFolder(int index) const
{
    if (index != -1) {
        return m_folderList.at(index).isDir();
    }
    return false;
}

void webdavfolderlistmodel::printError(QString errorMsg)
{
    qDebug() << "webdavfolderlistmodel::printErrors()  errorMsg == " << errorMsg;

    m_errorMsg = errorMsg;

    emit errorOccured();
}

void webdavfolderlistmodel::replySkipRead()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(QObject::sender());
    if (reply==0)
        return;

    QByteArray ba = reply->readAll();

    qDebug() << "webdavfolderlistmodel::replySkipRead()   skipped " << ba.size() << " reply->url() == " << reply->url().toString(QUrl::RemoveUserInfo);
}