1 | /* |
2 | * Copyright (C) by Olivier Goffart <ogoffart@woboq.com> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, but |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
12 | * for more details. |
13 | */ |
14 | |
15 | #pragma once |
16 | |
17 | #include "account.h" |
18 | #include "accountstate.h" |
19 | |
20 | namespace OCC { |
21 | |
22 | /** |
23 | @brief The AccountManager class |
24 | @ingroup gui |
25 | */ |
26 | class AccountManager : public QObject |
27 | { |
28 | Q_OBJECT |
29 | public: |
30 | static AccountManager *instance(); |
31 | ~AccountManager() {} |
32 | |
33 | /** |
34 | * Saves the accounts to a given settings file |
35 | */ |
36 | void save(bool saveCredentials = true); |
37 | |
38 | /** |
39 | * Creates account objects from a given settings file. |
40 | * |
41 | * Returns false if there was an error reading the settings, |
42 | * but note that settings not existing is not an error. |
43 | */ |
44 | bool restore(); |
45 | |
46 | /** |
47 | * Add this account in the list of saved accounts. |
48 | * Typically called from the wizard |
49 | */ |
50 | AccountState *addAccount(const AccountPtr &newAccount); |
51 | |
52 | /** |
53 | * remove all accounts |
54 | */ |
55 | void shutdown(); |
56 | |
57 | /** |
58 | * Return a list of all accounts. |
59 | * (this is a list of QSharedPointer for internal reasons, one should normally not keep a copy of them) |
60 | */ |
61 | QList<AccountStatePtr> accounts() { return _accounts; } |
62 | |
63 | /** |
64 | * Return the account state pointer for an account identified by its display name |
65 | */ |
66 | AccountStatePtr account(const QString &name); |
67 | |
68 | /** |
69 | * Delete the AccountState |
70 | */ |
71 | void deleteAccount(AccountState *account); |
72 | |
73 | |
74 | /** |
75 | * Creates an account and sets up some basic handlers. |
76 | * Does *not* add the account to the account manager just yet. |
77 | */ |
78 | static AccountPtr createAccount(); |
79 | |
80 | /** |
81 | * Returns the list of settings keys that can't be read because |
82 | * they are from the future. |
83 | */ |
84 | static void backwardMigrationSettingsKeys(QStringList *deleteKeys, QStringList *ignoreKeys); |
85 | |
86 | private: |
87 | // saving and loading Account to settings |
88 | void saveAccountHelper(Account *account, QSettings &settings, bool saveCredentials = true); |
89 | AccountPtr loadAccountHelper(QSettings &settings); |
90 | |
91 | bool restoreFromLegacySettings(); |
92 | |
93 | bool isAccountIdAvailable(const QString &id) const; |
94 | QString generateFreeAccountId() const; |
95 | |
96 | // Adds an account to the tracked list, emitting accountAdded() |
97 | void addAccountState(AccountState *accountState); |
98 | |
99 | public slots: |
100 | /// Saves account data, not including the credentials |
101 | void saveAccount(Account *a); |
102 | |
103 | /// Saves account state data, not including the account |
104 | void saveAccountState(AccountState *a); |
105 | |
106 | |
107 | Q_SIGNALS: |
108 | void accountAdded(AccountState *account); |
109 | void accountRemoved(AccountState *account); |
110 | |
111 | private: |
112 | AccountManager() {} |
113 | QList<AccountStatePtr> _accounts; |
114 | /// Account ids from settings that weren't read |
115 | QSet<QString> _additionalBlockedAccountIds; |
116 | }; |
117 | } |
118 | |