source: SMSSender/frontend/gui/persistence/persistencefactory.cpp @ 372:820718c076ff

separation-frontend-backend
Last change on this file since 372:820718c076ff was 372:820718c076ff, checked in by Sämy Zehnder <saemy.zehnder@…>, 7 years ago
  • We now only search for setting files in the user's config directory when not using a debug version of the program to avoid accidential overwriting the user's real settings when having side-by-side installation of a stable and debug version.
File size: 3.5 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-2014, gorrión. See http://smssender.gorrion.ch
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 3 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, see <http://www.gnu.org/licenses/>.
17 */
18#include "persistencefactory.h"
19
20#include <QCoreApplication>
21#include <QDebug>
22#include <QDesktopServices>
23#include <QSqlDatabase>
24#include <QSqlError>
25
26#include "frontend/persistence/accountmanager.h"
27#include "frontend/persistence/contactmanager.h"
28#include "frontend/persistence/gatewaymanager.h"
29#include "frontend/persistence/groupmanager.h"
30#include "frontend/gui/persistence/mainstorage.h"
31#include "frontend/persistence/database.h"
32
33namespace Persistence {
34
35PersistenceFactory::PersistenceFactory()
36    : m_mainStorage(new MainStorage)
37{
38    initSettingsPath();
39
40    m_mainDatabase.reset(createDatabase(""));
41    m_mainDatabase->registerStorage(m_mainStorage.data());
42}
43
44AccountManager *PersistenceFactory::createAccountManager(const ::GatewayManager &gatewayManager) const {
45    return new AccountManager(m_mainDatabase.data(), mainStorage(), gatewayManager);
46}
47
48ContactManager *PersistenceFactory::createContactManager() const {
49    return new ContactManager(m_mainDatabase.data(), mainStorage());
50}
51
52GatewayManager *PersistenceFactory::createGatewayManager() const {
53    return new GatewayManager(m_mainStorage.data());
54}
55
56GroupManager *PersistenceFactory::createGroupManager(const ::ContactManager &contactManager) const {
57    return new GroupManager(m_mainDatabase.data(), mainStorage(), contactManager);
58}
59
60IMainStorage *PersistenceFactory::mainStorage() const {
61    return m_mainStorage.data();
62}
63
64QDir PersistenceFactory::settingsPath() const {
65    return m_settingsPath;
66}
67
68
69void PersistenceFactory::initSettingsPath() {
70    QDir settingsPath(qApp->applicationDirPath());
71    #if defined(Q_OS_WIN)
72        if (settingsPath.dirName().toLower() == "debug" || settingsPath.dirName().toLower() == "release")
73            settingsPath.cdUp();
74    #elif defined(Q_OS_MAC)
75        if (settingsPath.dirName() == "MacOS") {
76            settingsPath.cdUp();
77            settingsPath.cd("settings");
78        }
79    #endif
80    qDebug() << "Portable settings path: " + settingsPath.absolutePath();
81
82#ifndef QT_DEBUG
83    if (!settingsPath.exists("data.db")) {
84        QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/gorrion/SMSSender/";
85        settingsPath.mkpath(path);
86        settingsPath.setPath(path);
87    }
88#endif
89
90    qDebug() << "Using " + settingsPath.absolutePath() + " as settings path";
91    m_settingsPath = settingsPath;
92}
93
94
95IDatabase *PersistenceFactory::createDatabase(const QString &name) const {
96    if (name.contains(QRegExp("\\W"))) {
97        qFatal("A database's name is not allowed to contain non-word chars: %s", qPrintable(name));
98    }
99
100    QString filename = settingsPath().absoluteFilePath("data" + (name.isEmpty() ? "" : "-" + name.toLower()) + ".db");
101    return new Database(name, filename);
102}
103
104}
Note: See TracBrowser for help on using the repository browser.