~pete-woods/storage-framework/cmake-extras-compatibility

« back to all changes in this revision

Viewing changes to include/unity/storage/qt/internal/ItemImpl.h

  • Committer: Bileto Bot
  • Date: 2016-11-04 12:21:58 UTC
  • mfrom: (12.1.1 merge-devel)
  • Revision ID: ci-train-bot@canonical.com-20161104122158-hu8h9qyg3vm129t5
* Added v2 of the client-side API.
* Updated server-side API to tell the provider which metadata to return.
* Update provider API to manager ProviderBase class as a shared_ptr.
* Update client to discover ownCloud/Nextcloud and OneDrive accounts.
* Add match_etag argument to Download() D-Bus method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors: Michi Henning <michi.henning@canonical.com>
 
17
 */
 
18
 
 
19
#pragma once
 
20
 
 
21
#include <unity/storage/internal/ItemMetadata.h>
 
22
#include <unity/storage/qt/Account.h>
 
23
#include <unity/storage/qt/internal/AccountImpl.h>
 
24
#include <unity/storage/qt/internal/RuntimeImpl.h>
 
25
#include <unity/storage/qt/internal/StorageErrorImpl.h>
 
26
#include <unity/storage/qt/Item.h>
 
27
 
 
28
namespace unity
 
29
{
 
30
namespace storage
 
31
{
 
32
namespace qt
 
33
{
 
34
namespace internal
 
35
{
 
36
 
 
37
class AccountImpl;
 
38
class RuntimeImpl;
 
39
 
 
40
class ItemImpl : public std::enable_shared_from_this<ItemImpl>
 
41
{
 
42
public:
 
43
    ItemImpl();
 
44
    ItemImpl(storage::internal::ItemMetadata const& md,
 
45
             std::shared_ptr<AccountImpl> const& account_impl);
 
46
    ItemImpl(ItemImpl const&) = default;
 
47
    ItemImpl(ItemImpl&&) = delete;
 
48
    ~ItemImpl() = default;
 
49
    ItemImpl& operator=(ItemImpl const&) = default;
 
50
    ItemImpl& operator=(ItemImpl&&) = delete;
 
51
 
 
52
    QString itemId() const;
 
53
    QString name() const;
 
54
    Account account() const;
 
55
    QString etag() const;
 
56
    Item::Type type() const;
 
57
    QVariantMap metadata() const;
 
58
    qint64 sizeInBytes() const;
 
59
    QDateTime lastModifiedTime() const;
 
60
    QList<QString> parentIds() const;
 
61
 
 
62
    ItemListJob* parents(QStringList const& keys) const;
 
63
    ItemJob* copy(Item const& newParent, QString const& newName, QStringList const& keys) const;
 
64
    ItemJob* move(Item const& newParent, QString const& newName, QStringList const& keys) const;
 
65
    VoidJob* deleteItem() const;
 
66
    Uploader* createUploader(Item::ConflictPolicy policy, qint64 sizeInBytes, QStringList const& keys) const;
 
67
    Downloader* createDownloader(Item::ConflictPolicy policy) const;
 
68
    ItemListJob* list(QStringList const& keys) const;
 
69
    ItemListJob* lookup(QString const& name, QStringList const& keys) const;
 
70
    ItemJob* createFolder(QString const& name, QStringList const& keys) const;
 
71
    Uploader* createFile(QString const& name) const;
 
72
    Uploader* createFile(QString const& name,
 
73
                         Item::ConflictPolicy policy,
 
74
                         qint64 sizeInBytes,
 
75
                         QString const& contentType,
 
76
                         QStringList const& keys) const;
 
77
 
 
78
    bool operator==(ItemImpl const&) const;
 
79
    bool operator!=(ItemImpl const&) const;
 
80
    bool operator<(ItemImpl const&) const;
 
81
    bool operator<=(ItemImpl const&) const;
 
82
    bool operator>(ItemImpl const&) const;
 
83
    bool operator>=(ItemImpl const&) const;
 
84
 
 
85
    size_t hash() const;
 
86
 
 
87
    static Item make_item(QString const& method,
 
88
                          storage::internal::ItemMetadata const& md,
 
89
                          std::shared_ptr<AccountImpl> const& account_impl);
 
90
 
 
91
    std::shared_ptr<RuntimeImpl> runtime_impl() const;
 
92
    std::shared_ptr<AccountImpl> account_impl() const;
 
93
 
 
94
private:
 
95
    template<typename T>
 
96
    decltype(T::make_job(StorageError())) check_invalid_or_destroyed(QString const& method) const;
 
97
 
 
98
    template<typename T>
 
99
    decltype(T::make_job(StorageError())) check_copy_move_precondition(QString const& method,
 
100
                                                                       Item const& newParent,
 
101
                                                                       QString const& newName) const;
 
102
 
 
103
    bool is_valid_;
 
104
    storage::internal::ItemMetadata md_;
 
105
    std::shared_ptr<AccountImpl> account_impl_;
 
106
 
 
107
    friend class unity::storage::qt::Item;
 
108
};
 
109
 
 
110
template<typename T>
 
111
decltype(T::make_job(StorageError())) ItemImpl::check_invalid_or_destroyed(QString const& method) const
 
112
{
 
113
    if (!is_valid_)
 
114
    {
 
115
        auto e = StorageErrorImpl::logic_error(method + ": cannot create job from invalid item");
 
116
        return T::make_job(e);
 
117
    }
 
118
    auto runtime = account_impl_->runtime_impl();
 
119
    if (!runtime || !runtime->isValid())
 
120
    {
 
121
        auto e = StorageErrorImpl::runtime_destroyed_error(method + ": Runtime was destroyed previously");
 
122
        return T::make_job(e);
 
123
    }
 
124
    return nullptr;
 
125
}
 
126
 
 
127
template<typename T>
 
128
decltype(T::make_job(StorageError())) ItemImpl::check_copy_move_precondition(QString const& method,
 
129
                                                                             Item const& newParent,
 
130
                                                                             QString const& newName) const
 
131
{
 
132
    auto invalid_job = check_invalid_or_destroyed<T>(method);
 
133
    if (invalid_job)
 
134
    {
 
135
        return invalid_job;
 
136
    }
 
137
    if (!newParent.isValid())
 
138
    {
 
139
        auto e = StorageErrorImpl::invalid_argument_error(method + ": newParent is invalid");
 
140
        return T::make_job(e);
 
141
    }
 
142
    if (newName.isEmpty())
 
143
    {
 
144
        auto e = StorageErrorImpl::invalid_argument_error(method + ": newName cannot be empty");
 
145
        return T::make_job(e);
 
146
    }
 
147
    if (account() != newParent.account())
 
148
    {
 
149
        auto e = StorageErrorImpl::logic_error(method + ": source and target must belong to the same account");
 
150
        return T::make_job(e);
 
151
    }
 
152
    if (newParent.type() == Item::Type::File)
 
153
    {
 
154
        auto e = StorageErrorImpl::logic_error(method + ": newParent cannot be a file");
 
155
        return T::make_job(e);
 
156
    }
 
157
    return nullptr;
 
158
}
 
159
 
 
160
}  // namespace internal
 
161
}  // namespace qt
 
162
}  // namespace storage
 
163
}  // namespace unity