| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "src/feedlyserviceroot.h" |
| 4 | |
| 5 | #include "src/definitions.h" |
| 6 | #include "src/feedlyentrypoint.h" |
| 7 | #include "src/feedlynetwork.h" |
| 8 | #include "src/gui/formeditfeedlyaccount.h" |
| 9 | |
| 10 | #include <librssguard/database/databasequeries.h> |
| 11 | #include <librssguard/definitions/definitions.h> |
| 12 | #include <librssguard/exceptions/applicationexception.h> |
| 13 | #include <librssguard/exceptions/feedfetchexception.h> |
| 14 | #include <librssguard/exceptions/networkexception.h> |
| 15 | #include <librssguard/miscellaneous/application.h> |
| 16 | #include <librssguard/miscellaneous/textfactory.h> |
| 17 | #include <librssguard/services/abstract/labelsnode.h> |
| 18 | |
| 19 | #if defined(FEEDLY_OFFICIAL_SUPPORT) |
| 20 | #include <librssguard/network-web/oauth2service.h> |
| 21 | #endif |
| 22 | |
| 23 | FeedlyServiceRoot::FeedlyServiceRoot(RootItem* parent) : ServiceRoot(parent), m_network(new FeedlyNetwork(this)) { |
| 24 | setIcon(FeedlyEntryPoint().icon()); |
| 25 | m_network->setService(this); |
| 26 | } |
| 27 | |
| 28 | bool FeedlyServiceRoot::isSyncable() const { |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | bool FeedlyServiceRoot::canBeEdited() const { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | FormAccountDetails* FeedlyServiceRoot::accountSetupDialog() const { |
| 37 | return new FormEditFeedlyAccount(qApp->mainFormWidget()); |
| 38 | } |
| 39 | |
| 40 | void FeedlyServiceRoot::editItems(const QList<RootItem*>& items) { |
| 41 | if (items.first()->kind() == RootItem::Kind::ServiceRoot) { |
| 42 | QScopedPointer<FormEditFeedlyAccount> p(qobject_cast<FormEditFeedlyAccount*>(accountSetupDialog())); |
| 43 | |
| 44 | p->addEditAccount(this); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | ServiceRoot::editItems(items); |
| 49 | } |
| 50 | |
| 51 | QVariantHash FeedlyServiceRoot::customDatabaseData() const { |
| 52 | QVariantHash data = ServiceRoot::customDatabaseData(); |
| 53 | |
| 54 | data[QSL("username")] = m_network->username(); |
| 55 | data[QSL("dat")] = m_network->developerAccessToken(); |
| 56 | |
| 57 | #if defined(FEEDLY_OFFICIAL_SUPPORT) |
| 58 | data[QSL("refresh_token")] = m_network->oauth()->refreshToken(); |
| 59 | #endif |
| 60 | |
| 61 | data[QSL("batch_size")] = m_network->batchSize(); |
| 62 | data[QSL("download_only_unread")] = m_network->downloadOnlyUnreadMessages(); |
| 63 | data[QSL("intelligent_synchronization")] = m_network->intelligentSynchronization(); |
| 64 | |
| 65 | return data; |
| 66 | } |
| 67 | |
| 68 | void FeedlyServiceRoot::setCustomDatabaseData(const QVariantHash& data) { |
| 69 | ServiceRoot::setCustomDatabaseData(data); |
| 70 | |
| 71 | m_network->setUsername(data[QSL("username")].toString()); |
| 72 | m_network->setDeveloperAccessToken(data[QSL("dat")].toString()); |
| 73 | |
| 74 | #if defined(FEEDLY_OFFICIAL_SUPPORT) |
| 75 | m_network->oauth()->setRefreshToken(data[QSL("refresh_token")].toString()); |
| 76 | #endif |
| 77 | |
| 78 | m_network->setBatchSize(data[QSL("batch_size")].toInt()); |
| 79 | m_network->setDownloadOnlyUnreadMessages(data[QSL("download_only_unread")].toBool()); |
| 80 | m_network->setIntelligentSynchronization(data[QSL("intelligent_synchronization")].toBool()); |
| 81 | } |
| 82 | |
| 83 | QList<Message> FeedlyServiceRoot::obtainNewMessages(Feed* feed, |
| 84 | const QHash<ServiceRoot::BagOfMessages, QStringList>& |
| 85 | stated_messages, |
| 86 | const QHash<QString, QStringList>& tagged_messages) { |
| 87 | Q_UNUSED(tagged_messages) |
| 88 | |
| 89 | try { |
| 90 | return m_network->messages(feed->customId(), stated_messages); |
| 91 | } |
| 92 | catch (const ApplicationException& ex) { |
| 93 | throw FeedFetchException(Feed::Status::NetworkError, ex.message()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void FeedlyServiceRoot::start(bool freshly_activated) { |
| 98 | if (!freshly_activated) { |
| 99 | DatabaseQueries::loadRootFromDatabase<Category, Feed>(this); |
| 100 | loadCacheFromFile(); |
| 101 | } |
| 102 | |
| 103 | updateTitle(); |
| 104 | |
| 105 | if (getSubTreeFeeds().isEmpty()) { |
| 106 | #if defined(FEEDLY_OFFICIAL_SUPPORT) |
| 107 | m_network->oauth()->login([this]() { |
| 108 | syncIn(); |
| 109 | }); |
| 110 | #else |
| 111 | syncIn(); |
| 112 | #endif |
| 113 | } |
| 114 | |
| 115 | #if defined(FEEDLY_OFFICIAL_SUPPORT) |
| 116 | else { |
| 117 | m_network->oauth()->login(); |
| 118 | } |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | QString FeedlyServiceRoot::code() const { |
| 123 | return FeedlyEntryPoint().code(); |
| 124 | } |
| 125 | |
| 126 | void FeedlyServiceRoot::saveAllCachedData(bool ignore_errors) { |
| 127 | auto msg_cache = takeMessageCache(); |
| 128 | QMapIterator<RootItem::ReadStatus, QStringList> i(msg_cache.m_cachedStatesRead); |
| 129 | |
| 130 | // Save the actual data read/unread. |
| 131 | while (i.hasNext()) { |
| 132 | i.next(); |
| 133 | auto key = i.key(); |
| 134 | QStringList ids = i.value(); |
| 135 | |
| 136 | if (!ids.isEmpty()) { |
| 137 | try { |
| 138 | network()->markers(key == RootItem::ReadStatus::Read ? QSL(FEEDLY_MARKERS_READ) : QSL(FEEDLY_MARKERS_UNREAD), |
| 139 | ids); |
| 140 | } |
| 141 | catch (const NetworkException& net_ex) { |
| 142 | qCriticalNN << LOGSEC_FEEDLY |
| 143 | << "Failed to synchronize read/unread state with error:" << QUOTE_W_SPACE(net_ex.message()) |
| 144 | << "and HTTP code" << QUOTE_W_SPACE_DOT(net_ex.networkError()); |
| 145 | |
| 146 | if (!ignore_errors) { |
| 147 | addMessageStatesToCache(ids, key); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | QMapIterator<RootItem::Importance, QList<Message>> j(msg_cache.m_cachedStatesImportant); |
| 154 | |
| 155 | // Save the actual data important/not important. |
| 156 | while (j.hasNext()) { |
| 157 | j.next(); |
| 158 | auto key = j.key(); |
| 159 | QList<Message> messages = j.value(); |
| 160 | |
| 161 | if (!messages.isEmpty()) { |
| 162 | QStringList ids = customIDsOfMessages(messages); |
| 163 | |
| 164 | try { |
| 165 | network()->markers(key == RootItem::Importance::Important ? FEEDLY_MARKERS_IMPORTANT |
| 166 | : FEEDLY_MARKERS_UNIMPORTANT, |
| 167 | ids); |
| 168 | } |
| 169 | catch (const NetworkException& net_ex) { |
| 170 | qCriticalNN << LOGSEC_FEEDLY << "Failed to synchronize important/unimportant state with error:" |
| 171 | << QUOTE_W_SPACE(net_ex.message()) << "and HTTP code" << QUOTE_W_SPACE_DOT(net_ex.networkError()); |
| 172 | |
| 173 | if (!ignore_errors) { |
| 174 | addMessageStatesToCache(messages, key); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | QMapIterator<QString, QStringList> k(msg_cache.m_cachedLabelAssignments); |
| 181 | |
| 182 | // Assign label for these messages. |
| 183 | while (k.hasNext()) { |
| 184 | k.next(); |
| 185 | auto label_custom_id = k.key(); |
| 186 | QStringList messages = k.value(); |
| 187 | |
| 188 | if (!messages.isEmpty()) { |
| 189 | try { |
| 190 | network()->tagEntries(label_custom_id, messages); |
| 191 | } |
| 192 | catch (const NetworkException& net_ex) { |
| 193 | qCriticalNN << LOGSEC_FEEDLY |
| 194 | << "Failed to synchronize tag assignments with error:" << QUOTE_W_SPACE(net_ex.message()) |
| 195 | << "and HTTP code" << QUOTE_W_SPACE_DOT(net_ex.networkError()); |
| 196 | |
| 197 | if (!ignore_errors) { |
| 198 | addLabelsAssignmentsToCache(messages, label_custom_id, true); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | QMapIterator<QString, QStringList> l(msg_cache.m_cachedLabelDeassignments); |
| 205 | |
| 206 | // Remove label from these messages. |
| 207 | while (l.hasNext()) { |
| 208 | l.next(); |
| 209 | auto label_custom_id = l.key(); |
| 210 | QStringList messages = l.value(); |
| 211 | |
| 212 | if (!messages.isEmpty()) { |
| 213 | try { |
| 214 | network()->untagEntries(label_custom_id, messages); |
| 215 | } |
| 216 | catch (const NetworkException& net_ex) { |
| 217 | qCriticalNN << LOGSEC_FEEDLY |
| 218 | << "Failed to synchronize tag DEassignments with error:" << QUOTE_W_SPACE(net_ex.message()) |
| 219 | << "and HTTP code" << QUOTE_W_SPACE_DOT(net_ex.networkError()); |
| 220 | |
| 221 | if (!ignore_errors) { |
| 222 | addLabelsAssignmentsToCache(messages, label_custom_id, false); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | ServiceRoot::LabelOperation FeedlyServiceRoot::supportedLabelOperations() const { |
| 230 | return ServiceRoot::LabelOperation::Synchronised; |
| 231 | } |
| 232 | |
| 233 | void FeedlyServiceRoot::updateTitle() { |
| 234 | setTitle(QSL("%1 (Feedly)").arg(TextFactory::extractUsernameFromEmail(m_network->username()))); |
| 235 | } |
| 236 | |
| 237 | RootItem* FeedlyServiceRoot::obtainNewTreeForSyncIn() const { |
| 238 | auto tree = m_network->collections(true); |
| 239 | auto* lblroot = new LabelsNode(tree); |
| 240 | auto labels = m_network->tags(); |
| 241 | |
| 242 | lblroot->setChildItems(labels); |
| 243 | tree->appendChild(lblroot); |
| 244 | |
| 245 | return tree; |
| 246 | } |
| 247 | |
| 248 | bool FeedlyServiceRoot::wantsBaggedIdsOfExistingMessages() const { |
| 249 | return m_network->intelligentSynchronization(); |
| 250 | } |