File: mockup_model.cpp

package info (click to toggle)
classified-ads 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,788 kB
  • sloc: cpp: 34,610; tcl: 1,175; xml: 64; makefile: 45
file content (171 lines) | stat: -rw-r--r-- 5,511 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*     -*-C++-*- -*-coding: utf-8-unix;-*-
  Classified Ads is Copyright (c) Antti Järvinen 2013-2018.

  This file is part of Classified Ads.

  Classified Ads is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  Classified Ads is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with Classified Ads; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "mockup_model.h"
#include "mockup_nodemodel.h"
#include "../log.h"
#include "../net/node.h"
#include "../datamodel/profilemodel.h"
#include <openssl/ssl.h> // library init 
#include <openssl/rand.h> // random seed things
#include "../datamodel/binaryfilemodel.h"
#include "../datamodel/camodel.h"
#include "../datamodel/privmsgmodel.h"
#include "../datamodel/profilecommentmodel.h"
#include "../datamodel/searchmodel.h"
#include "../datamodel/contentencryptionmodel.h"
#include "../datamodel/cadbrecordmodel.h"
#include "../datamodel/tclmodel.h"
#include <QFile>
#include <QDir>

MockUpModel::MockUpModel( MController *aController ) :
    iNetworkRequests(NULL),
    iController(aController),
    iProfileModel(NULL),
    iBinaryFileModel(NULL),
    iCAModel(NULL),
    iPrivMsgModel(NULL),
    iContentEncryptionModel(NULL),
    iProfileCommentModel(NULL),
    iSearchModel(NULL),
    iCaDbRecordModel(NULL),
    iTclModel(NULL) {
    LOG_STR("MockUpModel::MockUpModel in\n") ;

    SSL_load_error_strings() ;
    SSL_library_init() ;
    QFile randomFile("/dev/urandom") ;
    randomFile.open(QIODevice::ReadOnly) ; 
    QByteArray randomBytes = randomFile.read(1024) ;
    char *randomBytesPointer = randomBytes.data() ;
    RAND_seed(randomBytesPointer, 1024);
    iNodeModel = new MockUpNodeModel(iController) ;
    iProfileModel = new ProfileModel(aController, *this)  ;
    iBinaryFileModel = new BinaryFileModel(aController, *this) ;
    iCAModel = new ClassifiedAdsModel(aController, *this) ;
    iPrivMsgModel = new PrivMessageModel(aController, *this) ;
    iProfileCommentModel = new ProfileCommentModel(aController, *this) ;
    iContentEncryptionModel = new ContentEncryptionModel(aController, *this) ;
    iSearchModel = new SearchModel(*this,*iController) ;
    iSearchModel->setObjectName("CA SearchModel test") ;
    iCaDbRecordModel = new CaDbRecordModel(iController, *this) ; 
    iTclModel = new TclModel(iController, *this) ; 
    iConnections = new QList<Connection *>() ;
    iNetReqQueue = new QList <NetworkRequestExecutor::NetworkRequestQueueItem>();
    iDb = QSqlDatabase::addDatabase("QSQLITE", "ca-test-db") ; 
    QString path(QDir::home().path());
    path.append(QDir::separator()).append(".classified_ads");
    if (!QDir(path).exists()) {
        QDir().mkdir(path) ;
    }
    path.append(QDir::separator()).append("sqlite_db");
    path = QDir::toNativeSeparators(path);
    iDb.setDatabaseName(path);
    // Open database
    iDb.open() ;
    LOG_STR("MockUpModel::MockUpModel out\n") ;
}

MockUpModel::~MockUpModel() {
    iDb.close() ; 
    QSqlDatabase::removeDatabase("ca-test-db") ; 
    delete iNetworkRequests ;
    delete iNodeModel ;
    delete iProfileModel ;
    delete iBinaryFileModel ;
    delete iCAModel ;
    delete iPrivMsgModel ;
    delete iProfileCommentModel ;
    delete iContentEncryptionModel ;
    delete iSearchModel;
    delete iCaDbRecordModel;
    delete iTclModel;
    LOG_STR("MockUpModel::~MockUpModel\n") ;
}
void MockUpModel::addNetworkRequest(NetworkRequestExecutor::NetworkRequestQueueItem&
                                    aRequest) const {
    LOG_STR2(" MockUpModel::addNetworkRequest type %d\n", aRequest.iRequestType) ;
    if ( iNetworkRequests!= NULL ) {
        iNetworkRequests->append(aRequest) ;
    }
}

bool MockUpModel::lock() {
    return iMutex.tryLock(100*1000);
}

void MockUpModel::unlock() {
    iMutex.unlock() ;
}

MNodeModelProtocolInterface& MockUpModel::nodeModel() const {
    return *iNodeModel ;
}

ProfileModel& MockUpModel::profileModel() const {
    return *iProfileModel ;
}

BinaryFileModel& MockUpModel::binaryFileModel() const {
    return *iBinaryFileModel ;
}

ClassifiedAdsModel& MockUpModel::classifiedAdsModel() const {
    return *iCAModel ;
}

PrivMessageModel& MockUpModel::privateMessageModel() const {
    return *iPrivMsgModel ;
}

ContentEncryptionModel& MockUpModel::contentEncryptionModel() const {
    return *iContentEncryptionModel ;
}

ProfileCommentModel& MockUpModel::profileCommentModel() const {
    return *iProfileCommentModel ;
}

SearchModel* MockUpModel::searchModel() const {
    return iSearchModel ;
}

CaDbRecordModel* MockUpModel::caDbRecordModel() const {
    return iCaDbRecordModel ; 
}

TclModel& MockUpModel::tclModel() const {
    return *iTclModel ; 
}


QSqlDatabase MockUpModel::dataBaseConnection(bool* /* aIsFirstTime */ ) {
    return iDb ; 
}

const QList <Connection *>& MockUpModel::getConnections() const {
    return *iConnections ;
}

QList <NetworkRequestExecutor::NetworkRequestQueueItem>&
MockUpModel::getNetRequests() const {
    return *iNetReqQueue ;
}