| 1 | /* |
|---|
| 2 | * QuteCom, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2010 Mbdsys |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifndef OWUSERPROFILEFILESTORAGE_H |
|---|
| 21 | #define OWUSERPROFILEFILESTORAGE_H |
|---|
| 22 | |
|---|
| 23 | #include <coipmanager_base/coipmanagerbasedll.h> |
|---|
| 24 | |
|---|
| 25 | #include <util/StringList.h> |
|---|
| 26 | |
|---|
| 27 | #include <QtCore/QMutex> |
|---|
| 28 | |
|---|
| 29 | class UserProfile; |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * Saves the UserProfile object and its dependencies. |
|---|
| 33 | * |
|---|
| 34 | * @ingroup coipmanager |
|---|
| 35 | * @author Philippe Bernery |
|---|
| 36 | */ |
|---|
| 37 | class COIPMANAGER_BASE_API UserProfileFileStorage { |
|---|
| 38 | public: |
|---|
| 39 | |
|---|
| 40 | enum UserProfileStorageError { |
|---|
| 41 | /** The desired UserProfile has been loaded successfully. */ |
|---|
| 42 | UserProfileStorageErrorNoError, |
|---|
| 43 | |
|---|
| 44 | /** The given UserProfile does not exists. */ |
|---|
| 45 | UserProfileStorageErrorDoesNotExist, |
|---|
| 46 | |
|---|
| 47 | /** The desired UserProfile has been loaded from backups. */ |
|---|
| 48 | UserProfileStorageErrorLoadedFromBackup, |
|---|
| 49 | |
|---|
| 50 | /** The desired UserProfile cannot be loaded. */ |
|---|
| 51 | UserProfileStorageErrorCannotBeLoaded, |
|---|
| 52 | |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Gets the full path to the profile of id 'profileId'. |
|---|
| 57 | */ |
|---|
| 58 | static std::string getProfilePath(const std::string & profileId); |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Construct the UserProfileFileStorage class. |
|---|
| 62 | * |
|---|
| 63 | * @param pathToProfiles path to where profiles 'are'/'must be' saved |
|---|
| 64 | */ |
|---|
| 65 | UserProfileFileStorage(const std::string & pathToProfiles); |
|---|
| 66 | |
|---|
| 67 | ~UserProfileFileStorage(); |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Gets the ids of existing user profiles. |
|---|
| 71 | * |
|---|
| 72 | * These UserProfiles can then be loaded with getUserProfile. |
|---|
| 73 | * |
|---|
| 74 | * Reentrant |
|---|
| 75 | * |
|---|
| 76 | * @return vector of UserProfiles |
|---|
| 77 | */ |
|---|
| 78 | StringList getUserProfileIds() const; |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Gets the name of the UserProfile with id 'profileId'. |
|---|
| 82 | * |
|---|
| 83 | */ |
|---|
| 84 | std::string getNameOfUserProfile(const std::string & profileId) const; |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Will load the profile with id 'profileId'. |
|---|
| 88 | * |
|---|
| 89 | * If an error occured while trying the profile, |
|---|
| 90 | * the method will try to load the profile from |
|---|
| 91 | * the backup directory. |
|---|
| 92 | * |
|---|
| 93 | * ThreadSafe |
|---|
| 94 | * |
|---|
| 95 | */ |
|---|
| 96 | UserProfileStorageError load(const std::string & profileId, UserProfile & userProfile); |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Saves the given UserProfile. |
|---|
| 100 | * |
|---|
| 101 | * ThreadSafe. |
|---|
| 102 | * |
|---|
| 103 | */ |
|---|
| 104 | bool save(UserProfile & userProfile); |
|---|
| 105 | |
|---|
| 106 | private: |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * Gets the temporary save profile dir. |
|---|
| 110 | */ |
|---|
| 111 | static std::string getTempProfilePath(const std::string & profileId); |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Gets the full path to the backup profile named 'profileName'. |
|---|
| 115 | */ |
|---|
| 116 | static std::string getBackupProfilePath(const std::string & profileId); |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Tries to load a profile from the 'profiles' dir. |
|---|
| 120 | * |
|---|
| 121 | * @return false if error, true otherwise |
|---|
| 122 | */ |
|---|
| 123 | bool loadFromProfiles(const std::string & profileId, UserProfile & userProfile); |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Tries to load a profile from the 'backups' dir. |
|---|
| 127 | * |
|---|
| 128 | * @return false if error, true otherwise |
|---|
| 129 | */ |
|---|
| 130 | bool loadFromBackups(const std::string & profileId, UserProfile & userProfile); |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Loads a profile from a dir. |
|---|
| 134 | * |
|---|
| 135 | * @return false if error, true otherwise |
|---|
| 136 | */ |
|---|
| 137 | bool loadFromDir(const std::string & path, UserProfile & userProfile); |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Loads the UserProfile object from url/userprofile.xml |
|---|
| 141 | * |
|---|
| 142 | * @return false if error, true otherwise |
|---|
| 143 | */ |
|---|
| 144 | bool loadProfile(const std::string & url, UserProfile & userProfile); |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Saves the UserProfile object in url/userprofile.xml |
|---|
| 148 | * |
|---|
| 149 | * @return false if error, true otherwise |
|---|
| 150 | */ |
|---|
| 151 | bool saveProfile(const std::string & url, const UserProfile & userProfile); |
|---|
| 152 | |
|---|
| 153 | /** For thread-safe operations. */ |
|---|
| 154 | static QMutex * _mutex; |
|---|
| 155 | |
|---|
| 156 | /** Path to profiles. */ |
|---|
| 157 | static std::string _pathToProfiles; |
|---|
| 158 | }; |
|---|
| 159 | |
|---|
| 160 | #endif //OWUSERPROFILEFILESTORAGE_H |
|---|