Changeset View
Changeset View
Standalone View
Standalone View
krusader/Synchronizer/synchronizerdirlist.cpp
Show All 28 Lines | |||||
29 | 29 | | |||
30 | #include <qplatformdefs.h> | 30 | #include <qplatformdefs.h> | ||
31 | // QtCore | 31 | // QtCore | ||
32 | #include <QDir> | 32 | #include <QDir> | ||
33 | // QtWidgets | 33 | // QtWidgets | ||
34 | #include <QApplication> | 34 | #include <QApplication> | ||
35 | 35 | | |||
36 | #include <KI18n/KLocalizedString> | 36 | #include <KI18n/KLocalizedString> | ||
37 | #include <KIOCore/KFileItem> | | |||
38 | #include <KIO/JobUiDelegate> | 37 | #include <KIO/JobUiDelegate> | ||
38 | #include <KIOCore/KFileItem> | ||||
39 | #include <KWidgetsAddons/KMessageBox> | 39 | #include <KWidgetsAddons/KMessageBox> | ||
40 | 40 | | |||
41 | #include "../krservices.h" | ||||
42 | #include "../FileSystem/fileitem.h" | ||||
41 | #include "../FileSystem/filesystem.h" | 43 | #include "../FileSystem/filesystem.h" | ||
42 | #include "../FileSystem/krpermhandler.h" | 44 | #include "../FileSystem/krpermhandler.h" | ||
43 | #include "../krservices.h" | | |||
44 | 45 | | |||
45 | SynchronizerDirList::SynchronizerDirList(QWidget *w, bool hidden) : QObject(), QHash<QString, FileItem *>(), fileIterator(0), | 46 | SynchronizerDirList::SynchronizerDirList(QWidget *w, bool hidden) | ||
46 | parentWidget(w), busy(false), result(false), ignoreHidden(hidden), currentUrl() | 47 | : QObject(), QHash<QString, FileItem *>(), fileIterator(0), parentWidget(w), busy(false), | ||
48 | result(false), ignoreHidden(hidden), currentUrl() | ||||
47 | { | 49 | { | ||
48 | } | 50 | } | ||
49 | 51 | | |||
50 | SynchronizerDirList::~SynchronizerDirList() | 52 | SynchronizerDirList::~SynchronizerDirList() | ||
51 | { | 53 | { | ||
52 | if (fileIterator) | 54 | if (fileIterator) | ||
53 | delete fileIterator; | 55 | delete fileIterator; | ||
54 | 56 | | |||
55 | QHashIterator< QString, FileItem *> lit(*this); | 57 | QHashIterator<QString, FileItem *> lit(*this); | ||
56 | while (lit.hasNext()) | 58 | while (lit.hasNext()) | ||
57 | delete lit.next().value(); | 59 | delete lit.next().value(); | ||
58 | } | 60 | } | ||
59 | 61 | | |||
60 | FileItem *SynchronizerDirList::search(const QString &name, bool ignoreCase) | 62 | FileItem *SynchronizerDirList::search(const QString &name, bool ignoreCase) | ||
61 | { | 63 | { | ||
62 | if (!ignoreCase) { | 64 | if (!ignoreCase) { | ||
63 | if (!contains(name)) | 65 | if (!contains(name)) | ||
64 | return 0; | 66 | return 0; | ||
65 | return (*this)[ name ]; | 67 | return (*this)[name]; | ||
66 | } | 68 | } | ||
67 | 69 | | |||
68 | QHashIterator<QString, FileItem *> iter(*this); | 70 | QHashIterator<QString, FileItem *> iter(*this); | ||
69 | iter.toFront(); | 71 | iter.toFront(); | ||
70 | 72 | | |||
71 | QString file = name.toLower(); | 73 | QString file = name.toLower(); | ||
72 | 74 | | |||
75 | // TODO use a map | ||||
73 | while (iter.hasNext()) { | 76 | while (iter.hasNext()) { | ||
74 | FileItem *item = iter.next().value(); | 77 | FileItem *item = iter.next().value(); | ||
75 | if (file == item->getName().toLower()) | 78 | if (file == item->getName().toLower()) | ||
76 | return item; | 79 | return item; | ||
77 | } | 80 | } | ||
78 | return 0; | 81 | return 0; | ||
79 | } | 82 | } | ||
80 | 83 | | |||
81 | FileItem *SynchronizerDirList::first() | 84 | FileItem *SynchronizerDirList::first() | ||
82 | { | 85 | { | ||
83 | if (fileIterator == 0) | 86 | if (fileIterator == 0) | ||
84 | fileIterator = new QHashIterator<QString, FileItem *> (*this); | 87 | fileIterator = new QHashIterator<QString, FileItem *>(*this); | ||
85 | 88 | | |||
86 | fileIterator->toFront(); | 89 | fileIterator->toFront(); | ||
87 | if (fileIterator->hasNext()) | 90 | if (fileIterator->hasNext()) | ||
88 | return fileIterator->next().value(); | 91 | return fileIterator->next().value(); | ||
89 | return 0; | 92 | return 0; | ||
90 | } | 93 | } | ||
91 | 94 | | |||
92 | FileItem *SynchronizerDirList::next() | 95 | FileItem *SynchronizerDirList::next() | ||
93 | { | 96 | { | ||
94 | if (fileIterator == 0) | 97 | if (fileIterator == 0) | ||
95 | fileIterator = new QHashIterator<QString, FileItem *> (*this); | 98 | fileIterator = new QHashIterator<QString, FileItem *>(*this); | ||
96 | 99 | | |||
97 | if (fileIterator->hasNext()) | 100 | if (fileIterator->hasNext()) | ||
98 | return fileIterator->next().value(); | 101 | return fileIterator->next().value(); | ||
99 | return 0; | 102 | return 0; | ||
100 | } | 103 | } | ||
101 | 104 | | |||
102 | bool SynchronizerDirList::load(const QString &urlIn, bool wait) | 105 | bool SynchronizerDirList::load(const QUrl &url, bool wait) | ||
103 | { | 106 | { | ||
104 | if (busy) | 107 | if (busy) | ||
105 | return false; | 108 | return false; | ||
106 | 109 | | |||
107 | currentUrl = urlIn; | 110 | currentUrl = url; | ||
108 | const QUrl url = QUrl::fromUserInput(urlIn, QString(), QUrl::AssumeLocalFile); | | |||
109 | 111 | | |||
110 | QHashIterator< QString, FileItem *> lit(*this); | 112 | QHashIterator<QString, FileItem *> lit(*this); | ||
111 | while (lit.hasNext()) | 113 | while (lit.hasNext()) | ||
112 | delete lit.next().value(); | 114 | delete lit.next().value(); | ||
113 | clear(); | 115 | clear(); | ||
114 | if (fileIterator) { | 116 | if (fileIterator) { | ||
115 | delete fileIterator; | 117 | delete fileIterator; | ||
116 | fileIterator = 0; | 118 | fileIterator = 0; | ||
117 | } | 119 | } | ||
118 | 120 | | |||
119 | if (url.isLocalFile()) { | 121 | if (url.isLocalFile()) { | ||
120 | const QString dir = FileSystem::ensureTrailingSlash(url).path(); | 122 | const QString dir = FileSystem::ensureTrailingSlash(url).path(); | ||
121 | 123 | | |||
122 | QT_DIR* qdir = QT_OPENDIR(dir.toLocal8Bit()); | 124 | QT_DIR *qdir = QT_OPENDIR(dir.toLocal8Bit()); | ||
123 | if (!qdir) { | 125 | if (!qdir) { | ||
124 | KMessageBox::error(parentWidget, i18n("Cannot open the folder %1.", dir), i18n("Error")); | 126 | KMessageBox::error(parentWidget, i18n("Cannot open the folder %1.", dir), | ||
127 | i18n("Error")); | ||||
125 | emit finished(result = false); | 128 | emit finished(result = false); | ||
126 | return false; | 129 | return false; | ||
127 | } | 130 | } | ||
128 | 131 | | |||
129 | QT_DIRENT* dirEnt; | 132 | QT_DIRENT *dirEnt; | ||
130 | 133 | | |||
131 | while ((dirEnt = QT_READDIR(qdir)) != NULL) { | 134 | while ((dirEnt = QT_READDIR(qdir)) != NULL) { | ||
132 | const QString name = QString::fromLocal8Bit(dirEnt->d_name); | 135 | const QString name = QString::fromLocal8Bit(dirEnt->d_name); | ||
133 | 136 | | |||
134 | if (name == "." || name == "..") continue; | 137 | if (name == "." || name == "..") | ||
135 | if (ignoreHidden && name.startsWith('.')) continue; | 138 | continue; | ||
139 | if (ignoreHidden && name.startsWith('.')) | ||||
140 | continue; | ||||
136 | 141 | | |||
137 | FileItem *item = FileSystem::createLocalFileItem(name, dir); | 142 | FileItem *item = FileSystem::createLocalFileItem(name, dir); | ||
138 | 143 | | |||
139 | insert(name, item); | 144 | insert(name, item); | ||
140 | } | 145 | } | ||
141 | 146 | | |||
142 | QT_CLOSEDIR(qdir); | 147 | QT_CLOSEDIR(qdir); | ||
143 | emit finished(result = true); | 148 | emit finished(result = true); | ||
144 | return true; | 149 | return true; | ||
145 | } else { | 150 | } else { | ||
146 | KIO::Job *job = KIO::listDir(KrServices::escapeFileUrl(url), KIO::HideProgressInfo, true); | 151 | KIO::Job *job = KIO::listDir(KrServices::escapeFileUrl(url), KIO::HideProgressInfo, true); | ||
147 | connect(job, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)), | 152 | connect(job, SIGNAL(entries(KIO::Job *, KIO::UDSEntryList)), this, | ||
148 | this, SLOT(slotEntries(KIO::Job*,KIO::UDSEntryList))); | 153 | SLOT(slotEntries(KIO::Job *, KIO::UDSEntryList))); | ||
149 | connect(job, SIGNAL(result(KJob*)), | 154 | connect(job, SIGNAL(result(KJob *)), this, SLOT(slotListResult(KJob *))); | ||
150 | this, SLOT(slotListResult(KJob*))); | | |||
151 | busy = true; | 155 | busy = true; | ||
152 | 156 | | |||
153 | if (!wait) | 157 | if (!wait) | ||
154 | return true; | 158 | return true; | ||
155 | 159 | | |||
156 | while (busy) | 160 | while (busy) | ||
157 | qApp->processEvents(); | 161 | qApp->processEvents(); | ||
158 | return result; | 162 | return result; | ||
159 | } | 163 | } | ||
160 | } | 164 | } | ||
161 | 165 | | |||
162 | void SynchronizerDirList::slotEntries(KIO::Job *job, const KIO::UDSEntryList& entries) | 166 | void SynchronizerDirList::slotEntries(KIO::Job *job, const KIO::UDSEntryList &entries) | ||
163 | { | 167 | { | ||
164 | KIO::ListJob *listJob = static_cast<KIO::ListJob *>(job); | 168 | KIO::ListJob *listJob = static_cast<KIO::ListJob *>(job); | ||
165 | for (const KIO::UDSEntry entry : entries) { | 169 | for (const KIO::UDSEntry entry : entries) { | ||
166 | FileItem *item = FileSystem::createFileItemFromKIO(entry, listJob->url()); | 170 | FileItem *item = FileSystem::createFileItemFromKIO(entry, listJob->url()); | ||
167 | if (item) { | 171 | if (item) { | ||
168 | insert(item->getName(), item); | 172 | insert(item->getName(), item); | ||
169 | } | 173 | } | ||
170 | } | 174 | } | ||
171 | } | 175 | } | ||
172 | 176 | | |||
173 | void SynchronizerDirList::slotListResult(KJob *job) | 177 | void SynchronizerDirList::slotListResult(KJob *job) | ||
174 | { | 178 | { | ||
175 | busy = false; | 179 | busy = false; | ||
176 | if (job && job->error()) { | 180 | if (job && job->error()) { | ||
177 | job->uiDelegate()->showErrorMessage(); | 181 | job->uiDelegate()->showErrorMessage(); | ||
178 | emit finished(result = false); | 182 | emit finished(result = false); | ||
179 | return; | 183 | return; | ||
180 | } | 184 | } | ||
181 | emit finished(result = true); | 185 | emit finished(result = true); | ||
182 | } | 186 | } | ||
183 | |