00001 #include <QtXml/qdom.h>
00002
00003 #include "main.h"
00004 #include "appconfig.h"
00005 #include "mainwindow.h"
00006 #include "recordtabledata.h"
00007
00008 extern appconfig mytetraconfig;
00009
00010
00011
00012 recordtabledata::recordtabledata(QObject *pobj)
00013 {
00014 return;
00015 }
00016
00017
00018
00019 recordtabledata::~recordtabledata()
00020 {
00021 return;
00022 }
00023
00024
00025
00026 QString recordtabledata::get_field(QString name, int pos) const
00027 {
00028
00029 if(pos<0 || pos>=table.size())
00030 {
00031 QString i;
00032 i.setNum(pos);
00033 critical_error("In recordtabledata unavailable record index "+i);
00034 }
00035
00036 reclintype linetmp;
00037 linetmp=table.at(pos);
00038
00039
00040 if(linetmp.contains(name))
00041 return linetmp[name];
00042 else
00043 critical_error("In recordtabledata item not have field "+name);
00044
00045 return QString();
00046 }
00047
00048
00049
00050 void recordtabledata::set_field(QString name, QString value, int pos)
00051 {
00052
00053 if(pos<0 || pos>=table.size())
00054 {
00055 QString i;
00056 i.setNum(pos);
00057 critical_error("Unavailable record index "+i+" in table while field "+name+" try set to "+value);
00058 }
00059
00060
00061 (table[pos]).insert(name,value);
00062 }
00063
00064
00065
00066 QString recordtabledata::get_text(int pos) const
00067 {
00068
00069 if(pos<0 || pos>=table.size()) return QString();
00070
00071
00072 reclintype linetmp;
00073 linetmp=table.at(pos);
00074
00075
00076 QString filename;
00077 filename="./"+mytetraconfig.get_tetradir()+"/base/"+linetmp["dir"]+"/"+linetmp["file"];
00078
00079 QFile f(filename);
00080
00081
00082 if(!f.exists())
00083 critical_error("File "+filename+" not found");
00084
00085
00086 if(!f.open(QIODevice::ReadOnly))
00087 critical_error("File "+filename+" not readable. Check permission.");
00088
00089 return QString::fromUtf8(f.readAll());
00090 }
00091
00092
00093
00094 QMap<QString, QString> recordtabledata::get_fields(int pos) const
00095 {
00096
00097 if(pos<0 || pos>=table.size()) return QMap<QString, QString>();
00098
00099
00100 QList<QString> fieldnames;
00101 fieldnames << "name";
00102 fieldnames << "author";
00103 fieldnames << "url";
00104 fieldnames << "tags";
00105 fieldnames << "ctime";
00106 fieldnames << "dir";
00107 fieldnames << "file";
00108 fieldnames << "id";
00109
00110
00111 reclintype linetmp;
00112 linetmp=table.at(pos);
00113
00114 QMap<QString, QString> tmpr;
00115
00116
00117 for(int i=0;i<fieldnames.size();++i)
00118 if(linetmp.contains( fieldnames.at(i) ))
00119 tmpr[fieldnames.at(i)]=linetmp[fieldnames.at(i)];
00120 else
00121 critical_error("In recordtabledata get_record() not found field "+fieldnames.at(i));
00122
00123 return tmpr;
00124 }
00125
00126
00127
00128 QMap<QString, QString> recordtabledata::get_record_img(int pos) const
00129 {
00130
00131 if(pos<0 || pos>=table.size()) return QMap<QString, QString>();
00132
00133 QMap<QString, QString> record;
00134
00135 record=get_fields(pos);
00136 record["text"]=get_text(pos);
00137
00138 return record;
00139 }
00140
00141
00142
00143
00144 void recordtabledata::init(QDomElement dommodel)
00145 {
00146
00147
00148
00149
00150
00151 QDomElement *dml=&dommodel;
00152 setup_data_from_dom(dml);
00153 }
00154
00155
00156
00157 void recordtabledata::setup_data_from_dom(QDomElement *dommodel)
00158 {
00159
00160
00161
00162
00163
00164
00165 if(dommodel->tagName()!="recordtable") return;
00166
00167
00168 QDomElement *currentrec=&(dommodel->firstChildElement());
00169
00170 while(!currentrec->isNull())
00171 {
00172 if(currentrec->tagName()=="record")
00173 {
00174
00175 QDomNamedNodeMap attlist;
00176 attlist=currentrec->attributes();
00177
00178
00179 reclintype tmpline;
00180
00181
00182 int i;
00183 for(i=0;i<attlist.count();i++)
00184 {
00185 QDomAttr attcurr=attlist.item(i).toAttr();
00186
00187 QString name=attcurr.name();
00188 QString value=attcurr.value();
00189
00190 tmpline[name]=value;
00191
00192
00193
00194 }
00195
00196
00197 table << tmpline;
00198 }
00199
00200 currentrec=&(currentrec->nextSiblingElement());
00201 }
00202
00203 return;
00204 }
00205
00206
00207
00208 QDomDocument recordtabledata::export_data_to_dom(void)
00209 {
00210
00211 if(table.size()==0)return QDomDocument();
00212
00213 QDomDocument doc;
00214
00215 QDomElement recordtabledata = doc.createElement("recordtable");
00216 doc.appendChild(recordtabledata);
00217
00218
00219 int i;
00220 for(i=1;i<=table.size();i++)
00221 {
00222 QDomElement elem = doc.createElement("record");
00223
00224 reclintype linetmp;
00225 linetmp=table.at(i-1);
00226 elem.setAttribute("name", linetmp["name"]);
00227 elem.setAttribute("author",linetmp["author"]);
00228 elem.setAttribute("url", linetmp["url"]);
00229 elem.setAttribute("tags", linetmp["tags"]);
00230 elem.setAttribute("ctime", linetmp["ctime"]);
00231 elem.setAttribute("dir", linetmp["dir"]);
00232 elem.setAttribute("file", linetmp["file"]);
00233 elem.setAttribute("id", linetmp["id"]);
00234
00235
00236 doc.firstChild().appendChild(elem);
00237 }
00238
00239
00240
00241 return doc;
00242 }
00243
00244
00245
00246
00247
00248
00249 int recordtabledata::insert_new_record(int mode, int pos,
00250 QString name,
00251 QString author,
00252 QString url,
00253 QString tags,
00254 QString text)
00255 {
00256 qDebug() << "In recordtabledata method insert_new_record()";
00257
00258 QString namedir;
00259 QString namefile="text.html";
00260 QString namedirfull;
00261 QString namefilefull;
00262
00263
00264 namedir=mytetraconfig.get_lastnotenum_as_line();
00265 mytetraconfig.inc_lastnotenum();
00266
00267
00268 namedirfull="./"+mytetraconfig.get_tetradir()+"/base/"+namedir;
00269 namefilefull=namedirfull+"/"+namefile;
00270
00271
00272 QDir directory("./"+mytetraconfig.get_tetradir()+"/base");
00273 directory.mkdir(namedir);
00274
00275
00276 QFile wfile(namefilefull);
00277 if (!wfile.open(QIODevice::WriteOnly | QIODevice::Text))
00278 critical_error("Cant open file "+namefilefull+" for write.");
00279 QTextStream out(&wfile);
00280 out.setCodec("UTF-8");
00281 out << text;
00282
00283
00284 int idnum;
00285 idnum=mytetraconfig.get_lastidnum();
00286 mytetraconfig.inc_lastidnum();
00287 QString id;
00288 id.setNum(idnum);
00289
00290
00291 QDateTime ctime_dt=QDateTime::currentDateTime();
00292 QString ctime=ctime_dt.toString("yyyyMMddhhmmss");
00293
00294
00295 reclintype tmpline;
00296 tmpline["name"]=name;
00297 tmpline["author"]=author;
00298 tmpline["url"]=url;
00299 tmpline["tags"]=tags;
00300 tmpline["ctime"]=ctime;
00301 tmpline["dir"] =namedir;
00302 tmpline["file"]=namefile;
00303 tmpline["id"] =id;
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 if(mode==ADD_NEW_RECORD_TO_END)
00322 {
00323 table << tmpline;
00324 }
00325 else if(mode==ADD_NEW_RECORD_BEFORE)
00326 {
00327 table.insert(pos,tmpline);
00328 }
00329 else if(mode==ADD_NEW_RECORD_AFTER)
00330 {
00331 table.insert(pos+1,tmpline);
00332 }
00333
00334
00335
00336
00337
00338
00339
00340 int retpos=0;
00341 if(mode==ADD_NEW_RECORD_TO_END)retpos=table.size()-1;
00342 else if(mode==ADD_NEW_RECORD_BEFORE)retpos=pos;
00343 else if(mode==ADD_NEW_RECORD_AFTER)retpos=pos+1;
00344
00345 return retpos;
00346 }
00347
00348
00349 void recordtabledata::edit_record(int pos,
00350 QString name,
00351 QString author,
00352 QString url,
00353 QString tags)
00354 {
00355 qDebug() << "In recordtabledata method edit_record()";
00356
00357
00358 reclintype tmpline;
00359
00360 tmpline["name"] =name;
00361 tmpline["author"]=author;
00362 tmpline["url"] =url;
00363 tmpline["tags"] =tags;
00364
00365 tmpline["ctime"]=(table.at(pos))["ctime"];
00366 tmpline["dir"] =(table.at(pos))["dir"];
00367 tmpline["file"] =(table.at(pos))["file"];
00368 tmpline["id"] =(table.at(pos))["id"];
00369
00370 table.replace(pos,tmpline);
00371
00372
00373 }
00374
00375
00376
00377 void recordtabledata::delete_record(int i)
00378 {
00379 qDebug() << "Try delete record num " << i << " table count " << table.size();
00380
00381
00382 if(i>=table.size())return;
00383
00384
00385 QString dirfordelete=mytetraconfig.get_tetradir()+"/base/"+get_field("dir",i);
00386 qDebug() << "Remove dir " << dirfordelete;
00387 remove_dir( dirfordelete );
00388
00389
00390
00391
00392
00393 table.takeAt(i);
00394 qDebug() << "Delete record succesfull";
00395
00396
00397
00398 }
00399
00400
00401
00402 void recordtabledata::delete_records(QVector<int> delidx)
00403 {
00404
00405
00406 qSort(delidx.begin(), delidx.end(), qGreater<int>());
00407
00408 for(int i=0;i<delidx.count();i++)
00409 delete_record(delidx[i]);
00410 }
00411
00412
00413
00414 void recordtabledata::clear(void)
00415 {
00416 QVector<int> delrec;
00417
00418 for(int i=0;i<table.size();i++)delrec << i;
00419
00420 delete_records(delrec);
00421 }
00422
00423
00424
00425 int recordtabledata::size(void)
00426 {
00427 return table.size();
00428 }
00429
00430
00431
00432 void recordtabledata::moveup(int pos)
00433 {
00434 if(pos>0)
00435 {
00436
00437 table.move(pos,pos-1);
00438
00439
00440
00441
00442
00443 }
00444 }
00445
00446
00447
00448 void recordtabledata::movedn(int pos)
00449 {
00450 if(pos<table.count())
00451 {
00452
00453 table.move(pos,pos+1);
00454
00455
00456
00457
00458
00459 }
00460 }
00461