Changeset View
Changeset View
Standalone View
Standalone View
importer/importer.cpp
Show All 25 Lines | |||||
26 | #include <QDebug> | 26 | #include <QDebug> | ||
27 | #include <QUrl> | 27 | #include <QUrl> | ||
28 | #include <QTemporaryDir> | 28 | #include <QTemporaryDir> | ||
29 | 29 | | |||
30 | // KDE | 30 | // KDE | ||
31 | #include <KFileItem> | 31 | #include <KFileItem> | ||
32 | #include <KIO/CopyJob> | 32 | #include <KIO/CopyJob> | ||
33 | #include <KIO/DeleteJob> | 33 | #include <KIO/DeleteJob> | ||
34 | #include <KIO/MkpathJob> | ||||
34 | #include <KIO/Job> | 35 | #include <KIO/Job> | ||
36 | #include <kio/jobclasses.h> | ||||
35 | #include <KIO/JobUiDelegate> | 37 | #include <KIO/JobUiDelegate> | ||
36 | #include <KJobWidgets> | 38 | #include <KJobWidgets> | ||
37 | #include <KLocalizedString> | 39 | #include <KLocalizedString> | ||
38 | 40 | | |||
39 | // stdc++ | 41 | // stdc++ | ||
40 | #include <memory> | 42 | #include <memory> | ||
41 | 43 | | |||
42 | // Local | 44 | // Local | ||
43 | #include <fileutils.h> | 45 | #include <fileutils.h> | ||
44 | #include <filenameformater.h> | 46 | #include <filenameformater.h> | ||
45 | #include <lib/timeutils.h> | 47 | #include <lib/timeutils.h> | ||
46 | #include <QDir> | 48 | #include <QDir> | ||
47 | 49 | | |||
48 | namespace Gwenview | 50 | namespace Gwenview | ||
49 | { | 51 | { | ||
50 | 52 | | |||
51 | struct ImporterPrivate | 53 | struct ImporterPrivate | ||
52 | { | 54 | { | ||
53 | Importer* q; | 55 | Importer* q; | ||
54 | QWidget* mAuthWindow; | 56 | QWidget* mAuthWindow; | ||
55 | std::unique_ptr<FileNameFormater> mFileNameFormater; | 57 | std::unique_ptr<FileNameFormater> mFileNameFormater; | ||
56 | QUrl mTempImportDirUrl; | 58 | QUrl mTempImportDirUrl; | ||
57 | QTemporaryDir* mTempImportDir; | 59 | QTemporaryDir* mTempImportDir; | ||
60 | QUrl mDestinationDirUrl; | ||||
58 | 61 | | |||
59 | /* @defgroup reset Should be reset in start() | 62 | /* @defgroup reset Should be reset in start() | ||
60 | * @{ */ | 63 | * @{ */ | ||
61 | QList<QUrl> mUrlList; | 64 | QList<QUrl> mUrlList; | ||
62 | QList<QUrl> mImportedUrlList; | 65 | QList<QUrl> mImportedUrlList; | ||
63 | QList<QUrl> mSkippedUrlList; | 66 | QList<QUrl> mSkippedUrlList; | ||
64 | int mRenamedCount; | 67 | int mRenamedCount; | ||
65 | int mProgress; | 68 | int mProgress; | ||
66 | int mJobProgress; | 69 | int mJobProgress; | ||
67 | /* @} */ | 70 | /* @} */ | ||
68 | 71 | | |||
69 | QUrl mCurrentUrl; | 72 | QUrl mCurrentUrl; | ||
70 | 73 | | |||
71 | bool createImportDir(const QUrl& url) | 74 | bool createImportDir(const QUrl& url) | ||
72 | { | 75 | { | ||
73 | Q_ASSERT(url.isLocalFile()); | 76 | KIO::Job* job = KIO::mkpath(url, QUrl(), KIO::HideProgressInfo); | ||
74 | // FIXME: Support remote urls | 77 | KJobWidgets::setWindow(job,mAuthWindow); | ||
ngraham: `job, mAuthWindow` | |||||
bdevries: Another nice catch! | |||||
75 | 78 | if (!job->exec()) { | |||
76 | if (!QDir().mkpath(url.toLocalFile())) { | | |||
77 | emit q->error(i18n("Could not create destination folder.")); | 79 | emit q->error(i18n("Could not create destination folder.")); | ||
78 | return false; | 80 | return false; | ||
79 | } | 81 | } | ||
80 | 82 | | |||
83 | if (url.isLocalFile()) { | ||||
81 | QString tempDirPath = url.toLocalFile() + "/.gwenview_importer-XXXXXX"; | 84 | QString tempDirPath = url.toLocalFile() + "/.gwenview_importer-XXXXXX"; | ||
82 | mTempImportDir = new QTemporaryDir(tempDirPath); | 85 | mTempImportDir = new QTemporaryDir(tempDirPath); | ||
86 | } else { | ||||
87 | mTempImportDir = new QTemporaryDir(); | ||||
88 | } | ||||
83 | 89 | | |||
84 | if (!mTempImportDir->isValid()) { | 90 | if (!mTempImportDir->isValid()) { | ||
85 | emit q->error(i18n("Could not create temporary upload folder.")); | 91 | emit q->error(i18n("Could not create temporary upload folder.")); | ||
86 | return false; | 92 | return false; | ||
87 | } | 93 | } | ||
88 | 94 | | |||
89 | mTempImportDirUrl = QUrl::fromLocalFile(mTempImportDir->path() + '/'); | 95 | mTempImportDirUrl = QUrl::fromLocalFile(mTempImportDir->path() + '/'); | ||
90 | if (!mTempImportDirUrl.isValid()) { | 96 | if (!mTempImportDirUrl.isValid()) { | ||
91 | emit q->error(i18n("Could not create temporary upload folder.")); | 97 | emit q->error(i18n("Could not create temporary upload folder.")); | ||
92 | return false; | 98 | return false; | ||
93 | } | 99 | } | ||
94 | 100 | | |||
95 | return true; | 101 | return true; | ||
96 | } | 102 | } | ||
97 | 103 | | |||
98 | void importNext() | 104 | void importNext() | ||
99 | { | 105 | { | ||
100 | if (mUrlList.empty()) { | 106 | if (mUrlList.empty()) { | ||
101 | q->finalizeImport(); | 107 | q->finalizeImport(); | ||
102 | return; | 108 | return; | ||
103 | } | 109 | } | ||
104 | mCurrentUrl = mUrlList.takeFirst(); | 110 | mCurrentUrl = mUrlList.takeFirst(); | ||
105 | QUrl dst = mTempImportDirUrl; | 111 | QUrl dst = mTempImportDirUrl; | ||
106 | dst.setPath(dst.path() + mCurrentUrl.fileName()); | 112 | dst.setPath(dst.path() + mCurrentUrl.fileName()); | ||
107 | KIO::Job* job = KIO::copy(mCurrentUrl, dst, KIO::HideProgressInfo); | 113 | KIO::Job* job = KIO::copy(mCurrentUrl, dst, KIO::HideProgressInfo); | ||
ngraham: Seems unrelated; let's do this in a separate patch | |||||
Yes, it's is indeed not directly related. This solves a bug that already existed before this patch. I'll take it out and re-submit. bdevries: Yes, it's is indeed not directly related. This solves a bug that already existed before this… | |||||
108 | KJobWidgets::setWindow(job, mAuthWindow); | 114 | KJobWidgets::setWindow(job, mAuthWindow); | ||
109 | QObject::connect(job, &KJob::result, | 115 | QObject::connect(job, &KJob::result, | ||
110 | q, &Importer::slotCopyDone); | 116 | q, &Importer::slotCopyDone); | ||
111 | QObject::connect(job, SIGNAL(percent(KJob*,ulong)), | 117 | QObject::connect(job, SIGNAL(percent(KJob*,ulong)), | ||
112 | q, SLOT(slotPercent(KJob*,ulong))); | 118 | q, SLOT(slotPercent(KJob*,ulong))); | ||
113 | } | 119 | } | ||
114 | 120 | | |||
115 | void renameImportedUrl(const QUrl& src) | 121 | void renameImportedUrl(const QUrl& src) | ||
116 | { | 122 | { | ||
117 | QUrl dst = src.resolved(QUrl("..")); | 123 | QUrl dst = mDestinationDirUrl; | ||
118 | QString fileName; | 124 | QString fileName; | ||
119 | if (mFileNameFormater.get()) { | 125 | if (mFileNameFormater.get()) { | ||
120 | KFileItem item(src); | 126 | KFileItem item(src); | ||
121 | item.setDelayedMimeTypes(true); | 127 | item.setDelayedMimeTypes(true); | ||
122 | // Get the document time, but do not cache the result because the | 128 | // Get the document time, but do not cache the result because the | ||
123 | // 'src' url is temporary: if we import "foo/image.jpg" and | 129 | // 'src' url is temporary: if we import "foo/image.jpg" and | ||
124 | // "bar/image.jpg", both images will be temporarily saved in the | 130 | // "bar/image.jpg", both images will be temporarily saved in the | ||
125 | // 'src' url. | 131 | // 'src' url. | ||
126 | QDateTime dateTime = TimeUtils::dateTimeForFileItem(item, TimeUtils::SkipCache); | 132 | QDateTime dateTime = TimeUtils::dateTimeForFileItem(item, TimeUtils::SkipCache); | ||
127 | fileName = mFileNameFormater->format(src, dateTime); | 133 | fileName = mFileNameFormater->format(src, dateTime); | ||
128 | } else { | 134 | } else { | ||
129 | fileName = src.fileName(); | 135 | fileName = src.fileName(); | ||
130 | } | 136 | } | ||
131 | dst.setPath(dst.path() + fileName); | 137 | dst.setPath(dst.path() + '/' + fileName); | ||
132 | 138 | | |||
133 | FileUtils::RenameResult result = FileUtils::rename(src, dst, mAuthWindow); | 139 | FileUtils::RenameResult result = FileUtils::rename(src, dst, mAuthWindow); | ||
134 | switch (result) { | 140 | switch (result) { | ||
135 | case FileUtils::RenamedOK: | 141 | case FileUtils::RenamedOK: | ||
136 | mImportedUrlList << mCurrentUrl; | 142 | mImportedUrlList << mCurrentUrl; | ||
137 | break; | 143 | break; | ||
138 | case FileUtils::RenamedUnderNewName: | 144 | case FileUtils::RenamedUnderNewName: | ||
139 | mRenamedCount++; | 145 | mRenamedCount++; | ||
Show All 29 Lines | 174 | if (format.isEmpty()) { | |||
169 | d->mFileNameFormater.reset(nullptr); | 175 | d->mFileNameFormater.reset(nullptr); | ||
170 | } else { | 176 | } else { | ||
171 | d->mFileNameFormater.reset(new FileNameFormater(format)); | 177 | d->mFileNameFormater.reset(new FileNameFormater(format)); | ||
172 | } | 178 | } | ||
173 | } | 179 | } | ||
174 | 180 | | |||
175 | void Importer::start(const QList<QUrl>& list, const QUrl& destination) | 181 | void Importer::start(const QList<QUrl>& list, const QUrl& destination) | ||
176 | { | 182 | { | ||
183 | d->mDestinationDirUrl = destination; | ||||
177 | d->mUrlList = list; | 184 | d->mUrlList = list; | ||
178 | d->mImportedUrlList.clear(); | 185 | d->mImportedUrlList.clear(); | ||
179 | d->mSkippedUrlList.clear(); | 186 | d->mSkippedUrlList.clear(); | ||
180 | d->mRenamedCount = 0; | 187 | d->mRenamedCount = 0; | ||
181 | d->mProgress = 0; | 188 | d->mProgress = 0; | ||
182 | d->mJobProgress = 0; | 189 | d->mJobProgress = 0; | ||
183 | 190 | | |||
184 | emitProgressChanged(); | 191 | emitProgressChanged(); | ||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |
job, mAuthWindow