Merge lp:~ken-vandine/content-hub/content_item_move into lp:content-hub

Proposed by Ken VanDine
Status: Merged
Approved by: Bill Filler
Approved revision: 121
Merged at revision: 122
Proposed branch: lp:~ken-vandine/content-hub/content_item_move
Merge into: lp:content-hub
Diff against target: 90 lines (+64/-0)
2 files modified
import/Ubuntu/Content/contentitem.cpp (+62/-0)
import/Ubuntu/Content/contentitem.h (+2/-0)
To merge this branch: bzr merge lp:~ken-vandine/content-hub/content_item_move
Reviewer Review Type Date Requested Status
Bill Filler (community) Approve
Andrew Hayzen (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+226173@code.launchpad.net

Commit message

Added ContentItem.move function to allow pure QML apps to relocate local
files collected.

Description of the change

Added ContentItem.move function to allow pure QML apps to relocate local
files collected.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ken VanDine (ken-vandine) wrote :

Are there any related MPs required for this MP to build/function as expected? Please list.

 * No

Is your branch in sync with latest trunk (e.g. bzr pull lp:trunk -> no changes)

 * Yes

Did you perform an exploratory manual test run of your code change and any related functionality on device or emulator?

 * Yes

Did you successfully run all tests found in your component's Test Plan (https://wiki.ubuntu.com/Process/Merges/TestPlan/content-hub) on device or emulator?

 * Yes

If you changed the UI, was the change specified/approved by design?

 * No change

If you changed the packaging (debian), did you add a core-dev as a reviewer to this MP?

 * No change

Revision history for this message
Andrew Hayzen (ahayzen) wrote :

For the music-app, and possibly other apps, we may want to set the filename of the destination as well as the folder. So that we could save a file as ~/Music/Artist/Album/tracknum - title.ext

Therefore I propose the solution of move(dir, name) where dir is a mandatory parameter setting the target directory and name is an optional parameter. If name given it is used as the filename and if is not given the original filename is used.

Revision history for this message
Ken VanDine (ken-vandine) wrote :

> For the music-app, and possibly other apps, we may want to set the filename of
> the destination as well as the folder. So that we could save a file as
> ~/Music/Artist/Album/tracknum - title.ext
>
> Therefore I propose the solution of move(dir, name) where dir is a mandatory
> parameter setting the target directory and name is an optional parameter. If
> name given it is used as the filename and if is not given the original
> filename is used.

I that's reasonable, I'll do that.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ken VanDine (ken-vandine) wrote :

You can find an example that exercises the move function at https://code.launchpad.net/~ken-vandine/+junk/hub-importer-with-move

Revision history for this message
Andrew Hayzen (ahayzen) wrote :

This meets the requirements specified for the music-app above.

Thanks :)

review: Approve
Revision history for this message
Bill Filler (bfiller) wrote :

tested with the sample branch, works as expected

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'import/Ubuntu/Content/contentitem.cpp'
2--- import/Ubuntu/Content/contentitem.cpp 2014-06-18 13:33:30 +0000
3+++ import/Ubuntu/Content/contentitem.cpp 2014-07-10 18:05:11 +0000
4@@ -17,7 +17,9 @@
5 #include "contentitem.h"
6 #include "../../../src/com/ubuntu/content/debug.h"
7 #include <QMimeDatabase>
8+#include <QDir>
9 #include <QFile>
10+#include <QFileInfo>
11
12 /*!
13 * \qmltype ContentItem
14@@ -148,3 +150,63 @@
15 dataUri.append(QString::fromLatin1(data.toBase64()));
16 return QUrl(dataUri);
17 }
18+
19+/*!
20+ * \qmlmethod bool ContentItem::move(dir)
21+ * \brief If the url is a local file, move the file to \a dir
22+ *
23+ * If the move is successful, the url property will be changed
24+ * and onUrlChanged will be emitted.
25+ *
26+ * Returns true if the file was moved successfully, false
27+ * on error or if the url wasn't a local file.
28+ */
29+bool ContentItem::move(const QString &dir)
30+{
31+ TRACE() << Q_FUNC_INFO << "dir:" << dir;
32+ return (move(dir, nullptr));
33+}
34+
35+/*!
36+ * \qmlmethod bool ContentItem::move(dir, fileName)
37+ * \brief If the url is a local file, move the file to \a dir and
38+ * rename to \a fileName
39+ *
40+ * If the move is successful, the url property will be changed
41+ * and onUrlChanged will be emitted.
42+ *
43+ * Returns true if the file was moved successfully, false
44+ * on error or if the url wasn't a local file.
45+ */
46+bool ContentItem::move(const QString &dir, const QString &fileName)
47+{
48+ TRACE() << Q_FUNC_INFO << "dir:" << dir << "fileName:" << fileName;
49+
50+ QString path(m_item.url().toLocalFile());
51+
52+ if (!QFile::exists(path)) {
53+ qWarning() << "File not found:" << path;
54+ return false;
55+ }
56+
57+ QFileInfo fi(path);
58+ QDir d(dir);
59+ if (not d.exists())
60+ d.mkpath(d.absolutePath());
61+
62+ QString destFilePath = "";
63+ if (fileName.isEmpty())
64+ destFilePath = dir + QDir::separator() + fi.fileName();
65+ else
66+ destFilePath = dir + QDir::separator() + fileName;
67+
68+ TRACE() << Q_FUNC_INFO << "New path:" << destFilePath;
69+
70+ if (not QFile::rename(fi.absoluteFilePath(), destFilePath)) {
71+ qWarning() << "Failed to move content to:" << destFilePath;
72+ return false;
73+ }
74+
75+ setUrl(QUrl::fromLocalFile(destFilePath));
76+ return true;
77+}
78
79=== modified file 'import/Ubuntu/Content/contentitem.h'
80--- import/Ubuntu/Content/contentitem.h 2014-05-23 09:55:11 +0000
81+++ import/Ubuntu/Content/contentitem.h 2014-07-10 18:05:11 +0000
82@@ -42,6 +42,8 @@
83 void setItem(const com::ubuntu::content::Item &item);
84
85 Q_INVOKABLE QUrl toDataURI();
86+ Q_INVOKABLE bool move(const QString &dir);
87+ Q_INVOKABLE bool move(const QString &dir, const QString &fileName);
88
89 Q_SIGNALS:
90 void nameChanged();

Subscribers

People subscribed via source and target branches