Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/

SnapmaticPicture.cpp 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*****************************************************************************
  2. * gta5spv Grand Theft Auto Snapmatic Picture Viewer
  3. * Copyright (C) 2016-2018 Syping
  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 "SnapmaticPicture.h"
  19. #include <QStringBuilder>
  20. #include <QJsonDocument>
  21. #include <QJsonObject>
  22. #include <QStringList>
  23. #include <QVariantMap>
  24. #include <QTextCodec>
  25. #include <QJsonArray>
  26. #include <QFileInfo>
  27. #include <QPainter>
  28. #include <QString>
  29. #include <QBuffer>
  30. #include <QDebug>
  31. #include <QImage>
  32. #include <QSize>
  33. #include <QFile>
  34. #if QT_VERSION >= 0x050000
  35. #include <QSaveFile>
  36. #else
  37. #include "StandardPaths.h"
  38. #endif
  39. // PARSER ALLOCATIONS
  40. #define snapmaticHeaderLength 278
  41. #define snapmaticUsefulLength 260
  42. #define snapmaticFileMaxSize 528192
  43. #define jpegHeaderLineDifStr 2
  44. #define jpegHeaderLineDifLim 8
  45. #define jpegPreHeaderLength 14
  46. #define jpegPicStreamLength 524288
  47. #define jsonStreamLength 3076
  48. #define tideStreamLength 260
  49. // EDITOR ALLOCATIONS
  50. #define jpegStreamEditorBegin 292
  51. #define jsonStreamEditorBegin 524588
  52. #define jsonStreamEditorLength 3072
  53. #define titlStreamEditorBegin 527668
  54. #define titlStreamEditorLength 256
  55. #define titlStreamCharacterMax 39
  56. // LIMIT ALLOCATIONS
  57. #define jpegStreamLimitBegin 288
  58. // IMAGES VALUES
  59. #define snapmaticResolutionW 960
  60. #define snapmaticResolutionH 536
  61. #define snapmaticResolution QSize(snapmaticResolutionW, snapmaticResolutionH)
  62. SnapmaticPicture::SnapmaticPicture(const QString &fileName, QObject *parent) : QObject(parent), picFilePath(fileName)
  63. {
  64. reset();
  65. }
  66. SnapmaticPicture::~SnapmaticPicture()
  67. {
  68. }
  69. void SnapmaticPicture::reset()
  70. {
  71. // INIT PIC
  72. rawPicContent.clear();
  73. rawPicContent.squeeze();
  74. cachePicture = QImage();
  75. picExportFileName = QString();
  76. pictureHead = QString();
  77. pictureStr = QString();
  78. lastStep = QString();
  79. sortStr = QString();
  80. titlStr = QString();
  81. descStr = QString();
  82. // INIT PIC INTS
  83. jpegRawContentSizeE = 0;
  84. jpegRawContentSize = 0;
  85. // INIT PIC BOOLS
  86. isCustomFormat = false;
  87. isModernFormat = false;
  88. isFormatSwitch = false;
  89. isLoadedInRAM = false;
  90. lowRamMode = false;
  91. picOk = false;
  92. // INIT JSON
  93. jsonOk = false;
  94. jsonStr = QString();
  95. // SNAPMATIC DEFAULTS
  96. #ifdef GTA5SYNC_NOASSIST
  97. careSnapDefault = false;
  98. #else
  99. careSnapDefault = true;
  100. #endif
  101. // SNAPMATIC PROPERTIES
  102. localProperties = {};
  103. }
  104. bool SnapmaticPicture::preloadFile()
  105. {
  106. QFile *picFile = new QFile(picFilePath);
  107. picFileName = QFileInfo(picFilePath).fileName();
  108. bool g5eMode = false;
  109. isFormatSwitch = false;
  110. if (!picFile->open(QFile::ReadOnly))
  111. {
  112. lastStep = "1;/1,OpenFile," % convertDrawStringForLog(picFilePath);
  113. delete picFile;
  114. return false;
  115. }
  116. if (picFilePath.right(4) != QLatin1String(".g5e"))
  117. {
  118. rawPicContent = picFile->read(snapmaticFileMaxSize + 1024);
  119. picFile->close();
  120. delete picFile;
  121. if (rawPicContent.mid(1, 3) == QByteArray("G5E"))
  122. {
  123. isFormatSwitch = true;
  124. }
  125. else
  126. {
  127. isCustomFormat = false;
  128. isModernFormat = false;
  129. isLoadedInRAM = true;
  130. }
  131. }
  132. else
  133. {
  134. g5eMode = true;
  135. }
  136. if (g5eMode || isFormatSwitch)
  137. {
  138. QByteArray g5eContent;
  139. if (!isFormatSwitch)
  140. {
  141. g5eContent = picFile->read(snapmaticFileMaxSize + 1024);
  142. picFile->close();
  143. delete picFile;
  144. }
  145. else
  146. {
  147. g5eContent = rawPicContent;
  148. rawPicContent.clear();
  149. }
  150. // Set Custom Format
  151. isCustomFormat = true;
  152. // Reading g5e Content
  153. g5eContent.remove(0, 1);
  154. if (g5eContent.left(3) == QByteArray("G5E"))
  155. {
  156. g5eContent.remove(0, 3);
  157. if (g5eContent.left(2).toHex() == QByteArray("1000"))
  158. {
  159. g5eContent.remove(0, 2);
  160. if (g5eContent.left(3) == QByteArray("LEN"))
  161. {
  162. g5eContent.remove(0, 3);
  163. int fileNameLength = g5eContent.left(1).toHex().toInt();
  164. g5eContent.remove(0, 1);
  165. if (g5eContent.left(3) == QByteArray("FIL"))
  166. {
  167. g5eContent.remove(0, 3);
  168. picFileName = g5eContent.left(fileNameLength);
  169. g5eContent.remove(0, fileNameLength);
  170. if (g5eContent.left(3) == QByteArray("COM"))
  171. {
  172. g5eContent.remove(0, 3);
  173. rawPicContent = qUncompress(g5eContent);
  174. // Setting is values
  175. isModernFormat = false;
  176. isLoadedInRAM = true;
  177. }
  178. else
  179. {
  180. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",4,G5E_FORMATERROR";
  181. return false;
  182. }
  183. }
  184. else
  185. {
  186. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",3,G5E_FORMATERROR";
  187. return false;
  188. }
  189. }
  190. else
  191. {
  192. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",2,G5E_FORMATERROR";
  193. return false;
  194. }
  195. }
  196. else if (g5eContent.left(2).toHex() == QByteArray("3200"))
  197. {
  198. g5eContent.remove(0, 2);
  199. if (g5eContent.left(2).toHex() == QByteArray("0001"))
  200. {
  201. g5eContent.remove(0, 2);
  202. rawPicContent = qUncompress(g5eContent);
  203. // Setting is values
  204. isModernFormat = true;
  205. isLoadedInRAM = true;
  206. }
  207. else if (g5eContent.left(2).toHex() == QByteArray("0002"))
  208. {
  209. lastStep = "2;/4,ReadingFile," % convertDrawStringForLog(picFilePath) % ",2,G5E2_FORMATWRONG,G5E2_SGD";
  210. return false;
  211. }
  212. else
  213. {
  214. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",2,G5E2_MISSINGEXTENSION";
  215. return false;
  216. }
  217. }
  218. else
  219. {
  220. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",1,G5E_NOTCOMPATIBLE";
  221. return false;
  222. }
  223. }
  224. else
  225. {
  226. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",1,G5E_FORMATERROR";
  227. return false;
  228. }
  229. }
  230. emit preloaded();
  231. return true;
  232. }
  233. bool SnapmaticPicture::readingPicture(bool writeEnabled_, bool cacheEnabled_, bool fastLoad, bool lowRamMode_)
  234. {
  235. // Start opening file
  236. // lastStep is like currentStep
  237. // Set boolean values
  238. writeEnabled = writeEnabled_;
  239. cacheEnabled = cacheEnabled_;
  240. lowRamMode = lowRamMode_;
  241. if (!writeEnabled) { lowRamMode = false; } // Low RAM Mode only works when writeEnabled is true
  242. QIODevice *picStream;
  243. if (!isLoadedInRAM) { preloadFile(); }
  244. picStream = new QBuffer(&rawPicContent);
  245. picStream->open(QIODevice::ReadWrite);
  246. // Reading Snapmatic Header
  247. if (!picStream->isReadable())
  248. {
  249. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",1,NOHEADER";
  250. picStream->close();
  251. delete picStream;
  252. return false;
  253. }
  254. QByteArray snapmaticHeaderLine = picStream->read(snapmaticHeaderLength);
  255. pictureHead = getSnapmaticHeaderString(snapmaticHeaderLine);
  256. if (pictureHead == QLatin1String("MALFORMED"))
  257. {
  258. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",1,MALFORMEDHEADER";
  259. picStream->close();
  260. delete picStream;
  261. return false;
  262. }
  263. // Reading JPEG Header Line
  264. if (!picStream->isReadable())
  265. {
  266. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",2,NOHEADER";
  267. picStream->close();
  268. delete picStream;
  269. return false;
  270. }
  271. QByteArray jpegHeaderLine = picStream->read(jpegPreHeaderLength);
  272. // Checking for JPEG
  273. jpegHeaderLine.remove(0, jpegHeaderLineDifStr);
  274. if (jpegHeaderLine.left(4) != QByteArray("JPEG"))
  275. {
  276. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",2,NOJPEG";
  277. picStream->close();
  278. delete picStream;
  279. return false;
  280. }
  281. // Get JPEG Size Limit
  282. jpegHeaderLine.remove(0, jpegHeaderLineDifLim);
  283. QString jpegHeaderLineStr = QString::fromUtf8(jpegHeaderLine.toHex().remove(8 - 2, 2));
  284. QString hexadecimalStr = jpegHeaderLineStr.mid(4, 2) % jpegHeaderLineStr.mid(2, 2) % jpegHeaderLineStr.mid(0, 2);
  285. jpegRawContentSize = hexadecimalStr.toInt(0, 16);
  286. // Read JPEG Stream
  287. if (!picStream->isReadable())
  288. {
  289. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",2,NOPIC";
  290. picStream->close();
  291. delete picStream;
  292. return false;
  293. }
  294. QByteArray jpegRawContent = picStream->read(jpegPicStreamLength);
  295. if (cacheEnabled) picOk = cachePicture.loadFromData(jpegRawContent, "JPEG");
  296. if (!cacheEnabled)
  297. {
  298. QImage tempPicture;
  299. picOk = tempPicture.loadFromData(jpegRawContent, "JPEG");
  300. }
  301. else if (!fastLoad)
  302. {
  303. if (careSnapDefault)
  304. {
  305. QImage tempPicture = QImage(snapmaticResolution, QImage::Format_RGB888);
  306. QPainter tempPainter(&tempPicture);
  307. if (cachePicture.size() != snapmaticResolution)
  308. {
  309. tempPainter.drawImage(0, 0, cachePicture.scaled(snapmaticResolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  310. }
  311. else
  312. {
  313. tempPainter.drawImage(0, 0, cachePicture);
  314. }
  315. tempPainter.end();
  316. cachePicture = tempPicture;
  317. }
  318. else
  319. {
  320. QImage tempPicture = QImage(cachePicture.size(), QImage::Format_RGB888);
  321. QPainter tempPainter(&tempPicture);
  322. tempPainter.drawImage(0, 0, cachePicture);
  323. tempPainter.end();
  324. cachePicture = tempPicture;
  325. }
  326. }
  327. // Read JSON Stream
  328. if (!picStream->isReadable())
  329. {
  330. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",3,NOJSON";
  331. picStream->close();
  332. delete picStream;
  333. return false;
  334. }
  335. else if (picStream->read(4) != QByteArray("JSON"))
  336. {
  337. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",3,CTJSON";
  338. picStream->close();
  339. delete picStream;
  340. return false;
  341. }
  342. QByteArray jsonRawContent = picStream->read(jsonStreamLength);
  343. jsonStr = getSnapmaticJSONString(jsonRawContent);
  344. parseJsonContent(); // JSON parsing is own function
  345. if (!picStream->isReadable())
  346. {
  347. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",4,NOTITL";
  348. picStream->close();
  349. delete picStream;
  350. return false;
  351. }
  352. else if (picStream->read(4) != QByteArray("TITL"))
  353. {
  354. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",4,CTTITL";
  355. picStream->close();
  356. delete picStream;
  357. return false;
  358. }
  359. QByteArray titlRawContent = picStream->read(tideStreamLength);
  360. titlStr = getSnapmaticTIDEString(titlRawContent);
  361. if (!picStream->isReadable())
  362. {
  363. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",5,NODESC";
  364. picStream->close();
  365. delete picStream;
  366. return picOk;
  367. }
  368. else if (picStream->read(4) != QByteArray("DESC"))
  369. {
  370. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",5,CTDESC";
  371. picStream->close();
  372. delete picStream;
  373. return false;
  374. }
  375. QByteArray descRawContent = picStream->read(tideStreamLength);
  376. descStr = getSnapmaticTIDEString(descRawContent);
  377. updateStrings();
  378. picStream->close();
  379. delete picStream;
  380. if (!writeEnabled) { rawPicContent.clear(); }
  381. else if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
  382. emit loaded();
  383. return picOk;
  384. }
  385. QString SnapmaticPicture::getSnapmaticHeaderString(const QByteArray &snapmaticHeader)
  386. {
  387. QList<QByteArray> snapmaticBytesList = snapmaticHeader.left(snapmaticUsefulLength).split('\x01');
  388. if (snapmaticBytesList.length() < 2) { return QLatin1String("MALFORMED"); }
  389. QByteArray snapmaticBytes = snapmaticBytesList.at(1);
  390. return parseTitleString(snapmaticBytes, snapmaticBytes.length());
  391. }
  392. QString SnapmaticPicture::getSnapmaticJSONString(const QByteArray &jsonBytes)
  393. {
  394. QByteArray jsonUsefulBytes = jsonBytes;
  395. jsonUsefulBytes.replace('\x00', QString());
  396. jsonUsefulBytes.replace('\x0c', QString());
  397. return QString::fromUtf8(jsonUsefulBytes.trimmed());
  398. }
  399. QString SnapmaticPicture::getSnapmaticTIDEString(const QByteArray &tideBytes)
  400. {
  401. QByteArray tideUsefulBytes = tideBytes;
  402. tideUsefulBytes.remove(0,4);
  403. QList<QByteArray> tideUsefulBytesList = tideUsefulBytes.split('\x00');
  404. return QString::fromUtf8(tideUsefulBytesList.at(0).trimmed());
  405. }
  406. void SnapmaticPicture::updateStrings()
  407. {
  408. QString cmpPicTitl = titlStr;
  409. cmpPicTitl.replace('\"', "''");
  410. cmpPicTitl.replace(' ', '_');
  411. cmpPicTitl.replace(':', '-');
  412. cmpPicTitl.remove('\\');
  413. cmpPicTitl.remove('{');
  414. cmpPicTitl.remove('}');
  415. cmpPicTitl.remove('/');
  416. cmpPicTitl.remove('<');
  417. cmpPicTitl.remove('>');
  418. cmpPicTitl.remove('*');
  419. cmpPicTitl.remove('?');
  420. cmpPicTitl.remove('.');
  421. pictureStr = tr("PHOTO - %1").arg(localProperties.createdDateTime.toString("MM/dd/yy HH:mm:ss"));
  422. sortStr = localProperties.createdDateTime.toString("yyMMddHHmmss") % QString::number(localProperties.uid);
  423. QString exportStr = localProperties.createdDateTime.toString("yyyyMMdd") % "-" % QString::number(localProperties.uid);
  424. if (isModernFormat) { picFileName = "PGTA5" % QString::number(localProperties.uid); }
  425. picExportFileName = exportStr % "_" % cmpPicTitl;
  426. }
  427. bool SnapmaticPicture::readingPictureFromFile(const QString &fileName, bool writeEnabled_, bool cacheEnabled_, bool fastLoad, bool lowRamMode_)
  428. {
  429. if (!fileName.isEmpty())
  430. {
  431. picFilePath = fileName;
  432. return readingPicture(writeEnabled_, cacheEnabled_, fastLoad, lowRamMode_);
  433. }
  434. else
  435. {
  436. return false;
  437. }
  438. }
  439. bool SnapmaticPicture::setImage(const QImage &picture)
  440. {
  441. if (writeEnabled)
  442. {
  443. QImage altPicture;
  444. bool useAltPicture = false;
  445. if (picture.size() != snapmaticResolution && careSnapDefault)
  446. {
  447. altPicture = picture.scaled(snapmaticResolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  448. useAltPicture = true;
  449. }
  450. QByteArray picByteArray;
  451. int comLvl = 100;
  452. bool saveSuccess = false;
  453. while (comLvl != 0 && !saveSuccess)
  454. {
  455. QByteArray picByteArrayT;
  456. QBuffer picStreamT(&picByteArrayT);
  457. picStreamT.open(QIODevice::WriteOnly);
  458. if (useAltPicture) { saveSuccess = altPicture.save(&picStreamT, "JPEG", comLvl); }
  459. else { saveSuccess = picture.save(&picStreamT, "JPEG", comLvl); }
  460. picStreamT.close();
  461. if (saveSuccess)
  462. {
  463. if (picByteArrayT.length() > jpegPicStreamLength)
  464. {
  465. comLvl--;
  466. saveSuccess = false;
  467. }
  468. else
  469. {
  470. picByteArray = picByteArrayT;
  471. }
  472. }
  473. }
  474. if (saveSuccess) { return setPictureStream(picByteArray); }
  475. }
  476. return false;
  477. }
  478. bool SnapmaticPicture::setPictureStream(const QByteArray &streamArray) // clean method
  479. {
  480. if (writeEnabled)
  481. {
  482. QByteArray picByteArray = streamArray;
  483. if (lowRamMode) { rawPicContent = qUncompress(rawPicContent); }
  484. QBuffer snapmaticStream(&rawPicContent);
  485. snapmaticStream.open(QIODevice::ReadWrite);
  486. if (!snapmaticStream.seek(jpegStreamEditorBegin)) return false;
  487. if (picByteArray.length() > jpegPicStreamLength) return false;
  488. while (picByteArray.length() != jpegPicStreamLength)
  489. {
  490. picByteArray += '\x00';
  491. }
  492. int result = snapmaticStream.write(picByteArray);
  493. QString hexadecimalStr;
  494. hexadecimalStr.setNum(streamArray.length(), 16);
  495. while (hexadecimalStr.length() != 6)
  496. {
  497. hexadecimalStr.prepend('0');
  498. }
  499. hexadecimalStr = hexadecimalStr.mid(4, 2) % hexadecimalStr.mid(2, 2) % hexadecimalStr.mid(0, 2);
  500. bool updatedRawContentSize = false;
  501. if (snapmaticStream.seek(jpegStreamLimitBegin))
  502. {
  503. snapmaticStream.write(QByteArray::fromHex(hexadecimalStr.toUtf8()));
  504. updatedRawContentSize = true;
  505. }
  506. snapmaticStream.close();
  507. if (result != 0)
  508. {
  509. if (updatedRawContentSize) { jpegRawContentSize = streamArray.length(); }
  510. if (cacheEnabled)
  511. {
  512. QImage replacedPicture;
  513. replacedPicture.loadFromData(picByteArray);
  514. cachePicture = replacedPicture;
  515. }
  516. if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
  517. return true;
  518. }
  519. if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
  520. return false;
  521. }
  522. return false;
  523. }
  524. bool SnapmaticPicture::setPictureTitl(const QString &newTitle_)
  525. {
  526. if (writeEnabled)
  527. {
  528. QString newTitle = newTitle_;
  529. if (lowRamMode) { rawPicContent = qUncompress(rawPicContent); }
  530. QBuffer snapmaticStream(&rawPicContent);
  531. snapmaticStream.open(QIODevice::ReadWrite);
  532. if (!snapmaticStream.seek(titlStreamEditorBegin)) return false;
  533. if (newTitle.length() > titlStreamCharacterMax)
  534. {
  535. newTitle = newTitle.left(titlStreamCharacterMax);
  536. }
  537. QByteArray newTitleArray = newTitle.toUtf8();
  538. while (newTitleArray.length() != titlStreamEditorLength)
  539. {
  540. newTitleArray += '\x00';
  541. }
  542. int result = snapmaticStream.write(newTitleArray);
  543. snapmaticStream.close();
  544. if (result != 0)
  545. {
  546. titlStr = newTitle;
  547. if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
  548. return true;
  549. }
  550. if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
  551. return false;
  552. }
  553. return false;
  554. }
  555. QString SnapmaticPicture::getExportPictureFileName()
  556. {
  557. return picExportFileName;
  558. }
  559. QString SnapmaticPicture::getOriginalPictureFileName()
  560. {
  561. QString newPicFileName = picFileName;
  562. if (picFileName.right(4) == ".bak")
  563. {
  564. newPicFileName = QString(picFileName).remove(picFileName.length() - 4, 4);
  565. }
  566. if (picFileName.right(7) == ".hidden")
  567. {
  568. newPicFileName = QString(picFileName).remove(picFileName.length() - 7, 7);
  569. }
  570. return newPicFileName;
  571. }
  572. QString SnapmaticPicture::getOriginalPictureFilePath()
  573. {
  574. QString newPicFilePath = picFilePath;
  575. if (picFilePath.right(4) == ".bak")
  576. {
  577. newPicFilePath = QString(picFilePath).remove(picFilePath.length() - 4, 4);
  578. }
  579. if (picFilePath.right(7) == ".hidden")
  580. {
  581. newPicFilePath = QString(picFilePath).remove(picFilePath.length() - 7, 7);
  582. }
  583. return newPicFilePath;
  584. }
  585. QString SnapmaticPicture::getPictureFileName()
  586. {
  587. return picFileName;
  588. }
  589. QString SnapmaticPicture::getPictureFilePath()
  590. {
  591. return picFilePath;
  592. }
  593. QString SnapmaticPicture::getPictureSortStr()
  594. {
  595. return sortStr;
  596. }
  597. QString SnapmaticPicture::getPictureDesc()
  598. {
  599. return descStr;
  600. }
  601. QString SnapmaticPicture::getPictureTitl()
  602. {
  603. return titlStr;
  604. }
  605. QString SnapmaticPicture::getPictureHead()
  606. {
  607. return pictureHead;
  608. }
  609. QString SnapmaticPicture::getPictureStr()
  610. {
  611. return pictureStr;
  612. }
  613. QString SnapmaticPicture::getLastStep(bool readable)
  614. {
  615. if (readable)
  616. {
  617. QStringList lastStepList = lastStep.split(";/");
  618. if (lastStepList.length() < 2) { return lastStep; }
  619. bool intOk;
  620. //int stepNumber = lastStepList.at(0).toInt(&intOk);
  621. //if (!intOk) { return lastStep; }
  622. QStringList descStepList = lastStepList.at(1).split(",");
  623. if (descStepList.length() < 1) { return lastStep; }
  624. int argsCount = descStepList.at(0).toInt(&intOk);
  625. if (!intOk) { return lastStep; }
  626. if (argsCount == 1)
  627. {
  628. QString currentAction = descStepList.at(1);
  629. QString actionFile = descStepList.at(2);
  630. if (currentAction == "OpenFile")
  631. {
  632. return tr("open file %1").arg(actionFile);
  633. }
  634. }
  635. else if (argsCount == 3 || argsCount == 4)
  636. {
  637. QString currentAction = descStepList.at(1);
  638. QString actionFile = descStepList.at(2);
  639. //QString actionStep = descStepList.at(3);
  640. QString actionError = descStepList.at(4);
  641. QString actionError2;
  642. if (argsCount == 4) { actionError2 = descStepList.at(5); }
  643. if (currentAction == "ReadingFile")
  644. {
  645. QString readableError = actionError;
  646. if (actionError == "NOHEADER")
  647. {
  648. readableError = tr("header not exists");
  649. }
  650. else if (actionError == "MALFORMEDHEADER")
  651. {
  652. readableError = tr("header is malformed");
  653. }
  654. else if (actionError == "NOJPEG" || actionError == "NOPIC")
  655. {
  656. readableError = tr("picture not exists (%1)").arg(actionError);
  657. }
  658. else if (actionError == "NOJSON" || actionError == "CTJSON")
  659. {
  660. readableError = tr("JSON not exists (%1)").arg(actionError);
  661. }
  662. else if (actionError == "NOTITL" || actionError == "CTTITL")
  663. {
  664. readableError = tr("title not exists (%1)").arg(actionError);
  665. }
  666. else if (actionError == "NODESC" || actionError == "CTDESC")
  667. {
  668. readableError = tr("description not exists (%1)").arg(actionError);
  669. }
  670. else if (actionError == "JSONINCOMPLETE" && actionError2 == "JSONERROR")
  671. {
  672. readableError = tr("JSON is incomplete and malformed");
  673. }
  674. else if (actionError == "JSONINCOMPLETE")
  675. {
  676. readableError = tr("JSON is incomplete");
  677. }
  678. else if (actionError == "JSONERROR")
  679. {
  680. readableError = tr("JSON is malformed");
  681. }
  682. return tr("reading file %1 because of %2", "Example for %2: JSON is malformed error").arg(actionFile, readableError);
  683. }
  684. else
  685. {
  686. return lastStep;
  687. }
  688. }
  689. else
  690. {
  691. return lastStep;
  692. }
  693. }
  694. return lastStep;
  695. }
  696. QImage SnapmaticPicture::getImage(bool fastLoad)
  697. {
  698. if (cacheEnabled)
  699. {
  700. return cachePicture;
  701. }
  702. else if (writeEnabled)
  703. {
  704. bool fastLoadU = fastLoad;
  705. if (!careSnapDefault) { fastLoadU = true; }
  706. bool returnOk = false;
  707. QImage tempPicture;
  708. QImage returnPicture;
  709. if (!fastLoadU)
  710. {
  711. returnPicture = QImage(snapmaticResolution, QImage::Format_RGB888);
  712. }
  713. if (lowRamMode) { rawPicContent = qUncompress(rawPicContent); }
  714. QBuffer snapmaticStream(&rawPicContent);
  715. snapmaticStream.open(QIODevice::ReadOnly);
  716. if (snapmaticStream.seek(jpegStreamEditorBegin))
  717. {
  718. QByteArray jpegRawContent = snapmaticStream.read(jpegPicStreamLength);
  719. returnOk = tempPicture.loadFromData(jpegRawContent, "JPEG");
  720. }
  721. snapmaticStream.close();
  722. if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
  723. if (returnOk)
  724. {
  725. if (!fastLoadU)
  726. {
  727. QPainter returnPainter(&returnPicture);
  728. if (tempPicture.size() != snapmaticResolution)
  729. {
  730. returnPainter.drawImage(0, 0, tempPicture.scaled(snapmaticResolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  731. }
  732. else
  733. {
  734. returnPainter.drawImage(0, 0, tempPicture);
  735. }
  736. returnPainter.end();
  737. return returnPicture;
  738. }
  739. else
  740. {
  741. return tempPicture;
  742. }
  743. }
  744. }
  745. else
  746. {
  747. bool fastLoadU = fastLoad;
  748. if (!careSnapDefault) { fastLoadU = true; }
  749. bool returnOk = false;
  750. QImage tempPicture;
  751. QImage returnPicture;
  752. if (!fastLoadU)
  753. {
  754. returnPicture = QImage(snapmaticResolution, QImage::Format_RGB888);
  755. }
  756. QIODevice *picStream;
  757. QFile *picFile = new QFile(picFilePath);
  758. if (!picFile->open(QFile::ReadOnly))
  759. {
  760. lastStep = "1;/1,OpenFile," % convertDrawStringForLog(picFilePath);
  761. delete picFile;
  762. return QImage();
  763. }
  764. rawPicContent = picFile->read(snapmaticFileMaxSize);
  765. picFile->close();
  766. delete picFile;
  767. picStream = new QBuffer(&rawPicContent);
  768. picStream->open(QIODevice::ReadWrite);
  769. if (picStream->seek(jpegStreamEditorBegin))
  770. {
  771. QByteArray jpegRawContent = picStream->read(jpegPicStreamLength);
  772. returnOk = tempPicture.loadFromData(jpegRawContent, "JPEG");
  773. }
  774. picStream->close();
  775. delete picStream;
  776. rawPicContent.clear();
  777. rawPicContent.squeeze();
  778. if (returnOk)
  779. {
  780. if (!fastLoadU)
  781. {
  782. QPainter returnPainter(&returnPicture);
  783. if (tempPicture.size() != snapmaticResolution)
  784. {
  785. returnPainter.drawImage(0, 0, tempPicture.scaled(snapmaticResolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  786. }
  787. else
  788. {
  789. returnPainter.drawImage(0, 0, tempPicture);
  790. }
  791. returnPainter.end();
  792. return returnPicture;
  793. }
  794. else
  795. {
  796. return tempPicture;
  797. }
  798. }
  799. }
  800. return QImage();
  801. }
  802. QByteArray SnapmaticPicture::getPictureStream() // Incomplete because it just work in writeEnabled mode
  803. {
  804. QByteArray jpegRawContent;
  805. if (writeEnabled)
  806. {
  807. QBuffer *picStream = new QBuffer(&rawPicContent);
  808. picStream->open(QIODevice::ReadWrite);
  809. if (picStream->seek(jpegStreamEditorBegin))
  810. {
  811. jpegRawContent = picStream->read(jpegPicStreamLength);
  812. }
  813. delete picStream;
  814. }
  815. return jpegRawContent;
  816. }
  817. int SnapmaticPicture::getContentMaxLength()
  818. {
  819. return jpegRawContentSize;
  820. }
  821. bool SnapmaticPicture::isPicOk()
  822. {
  823. return picOk;
  824. }
  825. void SnapmaticPicture::clearCache()
  826. {
  827. cacheEnabled = false;
  828. cachePicture = QImage();
  829. }
  830. void SnapmaticPicture::emitUpdate()
  831. {
  832. emit updated();
  833. }
  834. void SnapmaticPicture::emitCustomSignal(const QString &signal)
  835. {
  836. emit customSignal(signal);
  837. }
  838. // JSON part
  839. bool SnapmaticPicture::isJsonOk()
  840. {
  841. return jsonOk;
  842. }
  843. QString SnapmaticPicture::getJsonStr()
  844. {
  845. return jsonStr;
  846. }
  847. SnapmaticProperties SnapmaticPicture::getSnapmaticProperties()
  848. {
  849. return localProperties;
  850. }
  851. void SnapmaticPicture::parseJsonContent()
  852. {
  853. QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toUtf8());
  854. QJsonObject jsonObject = jsonDocument.object();
  855. QVariantMap jsonMap = jsonObject.toVariantMap();
  856. bool jsonIncomplete = false;
  857. bool jsonError = false;
  858. if (jsonObject.contains("loc"))
  859. {
  860. if (jsonObject["loc"].isObject())
  861. {
  862. QJsonObject locObject = jsonObject["loc"].toObject();
  863. if (locObject.contains("x"))
  864. {
  865. if (locObject["x"].isDouble()) { localProperties.location.x = locObject["x"].toDouble(); }
  866. else { jsonError = true; }
  867. }
  868. else { jsonIncomplete = true; }
  869. if (locObject.contains("y"))
  870. {
  871. if (locObject["y"].isDouble()) { localProperties.location.y = locObject["y"].toDouble(); }
  872. else { jsonError = true; }
  873. }
  874. else { jsonIncomplete = true; }
  875. if (locObject.contains("z"))
  876. {
  877. if (locObject["z"].isDouble()) { localProperties.location.z = locObject["z"].toDouble(); }
  878. else { jsonError = true; }
  879. }
  880. else { jsonIncomplete = true; }
  881. }
  882. else { jsonError = true; }
  883. }
  884. else { jsonIncomplete = true; }
  885. if (jsonObject.contains("uid"))
  886. {
  887. bool uidOk;
  888. localProperties.uid = jsonMap["uid"].toInt(&uidOk);
  889. if (!uidOk) { jsonError = true; }
  890. }
  891. else { jsonIncomplete = true; }
  892. if (jsonObject.contains("area"))
  893. {
  894. if (jsonObject["area"].isString()) { localProperties.location.area = jsonObject["area"].toString(); }
  895. else { jsonError = true; }
  896. }
  897. else { jsonIncomplete = true; }
  898. if (jsonObject.contains("crewid"))
  899. {
  900. bool crewIDOk;
  901. localProperties.crewID = jsonMap["crewid"].toInt(&crewIDOk);
  902. if (!crewIDOk) { jsonError = true; }
  903. }
  904. else { jsonIncomplete = true; }
  905. if (jsonObject.contains("street"))
  906. {
  907. bool streetIDOk;
  908. localProperties.streetID = jsonMap["street"].toInt(&streetIDOk);
  909. if (!streetIDOk) { jsonError = true; }
  910. }
  911. else { jsonIncomplete = true; }
  912. if (jsonObject.contains("creat"))
  913. {
  914. bool timestampOk;
  915. QDateTime createdTimestamp;
  916. localProperties.createdTimestamp = jsonMap["creat"].toUInt(&timestampOk);
  917. createdTimestamp.setTime_t(localProperties.createdTimestamp);
  918. localProperties.createdDateTime = createdTimestamp;
  919. if (!timestampOk) { jsonError = true; }
  920. }
  921. else { jsonIncomplete = true; }
  922. if (jsonObject.contains("plyrs"))
  923. {
  924. if (jsonObject["plyrs"].isArray()) { localProperties.playersList = jsonMap["plyrs"].toStringList(); }
  925. else { jsonError = true; }
  926. }
  927. // else { jsonIncomplete = true; } // 2016 Snapmatic pictures left out plyrs when none are captured, so don't force exists on that one
  928. if (jsonObject.contains("meme"))
  929. {
  930. if (jsonObject["meme"].isBool()) { localProperties.isMeme = jsonObject["meme"].toBool(); }
  931. else { jsonError = true; }
  932. }
  933. else { jsonIncomplete = true; }
  934. if (jsonObject.contains("mug"))
  935. {
  936. if (jsonObject["mug"].isBool()) { localProperties.isMug = jsonObject["mug"].toBool(); }
  937. else { jsonError = true; }
  938. }
  939. else { jsonIncomplete = true; }
  940. if (jsonObject.contains("slf"))
  941. {
  942. if (jsonObject["slf"].isBool()) { localProperties.isSelfie = jsonObject["slf"].toBool(); }
  943. else { jsonError = true; }
  944. }
  945. else { jsonIncomplete = true; }
  946. if (jsonObject.contains("drctr"))
  947. {
  948. if (jsonObject["drctr"].isBool()) { localProperties.isFromDirector = jsonObject["drctr"].toBool(); }
  949. else { jsonError = true; }
  950. }
  951. else { jsonIncomplete = true; }
  952. if (jsonObject.contains("rsedtr"))
  953. {
  954. if (jsonObject["rsedtr"].isBool()) { localProperties.isFromRSEditor = jsonObject["rsedtr"].toBool(); }
  955. else { jsonError = true; }
  956. }
  957. // else { jsonIncomplete = true; } // Game release Snapmatic pictures prior May 2015 left out rsedtr, so don't force exists on that one
  958. if (!jsonIncomplete && !jsonError)
  959. {
  960. jsonOk = true;
  961. }
  962. else
  963. {
  964. if (jsonIncomplete && jsonError)
  965. {
  966. lastStep = "2;/4,ReadingFile," % convertDrawStringForLog(picFilePath) % ",3,JSONINCOMPLETE,JSONERROR";
  967. }
  968. else if (jsonIncomplete)
  969. {
  970. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",3,JSONINCOMPLETE";
  971. }
  972. else if (jsonError)
  973. {
  974. lastStep = "2;/3,ReadingFile," % convertDrawStringForLog(picFilePath) % ",3,JSONERROR";
  975. }
  976. jsonOk = false;
  977. }
  978. }
  979. bool SnapmaticPicture::setSnapmaticProperties(SnapmaticProperties properties)
  980. {
  981. QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toUtf8());
  982. QJsonObject jsonObject = jsonDocument.object();
  983. QJsonObject locObject;
  984. locObject["x"] = properties.location.x;
  985. locObject["y"] = properties.location.y;
  986. locObject["z"] = properties.location.z;
  987. jsonObject["loc"] = locObject;
  988. jsonObject["uid"] = properties.uid;
  989. jsonObject["area"] = properties.location.area;
  990. jsonObject["crewid"] = properties.crewID;
  991. jsonObject["street"] = properties.streetID;
  992. jsonObject["creat"] = QJsonValue::fromVariant(properties.createdTimestamp);
  993. jsonObject["plyrs"] = QJsonValue::fromVariant(properties.playersList);
  994. jsonObject["meme"] = properties.isMeme;
  995. jsonObject["mug"] = properties.isMug;
  996. jsonObject["slf"] = properties.isSelfie;
  997. jsonObject["drctr"] = properties.isFromDirector;
  998. jsonObject["rsedtr"] = properties.isFromRSEditor;
  999. jsonDocument.setObject(jsonObject);
  1000. if (setJsonStr(QString::fromUtf8(jsonDocument.toJson(QJsonDocument::Compact))))
  1001. {
  1002. localProperties = properties;
  1003. return true;
  1004. }
  1005. return false;
  1006. }
  1007. bool SnapmaticPicture::setJsonStr(const QString &newJsonStr, bool updateProperties)
  1008. {
  1009. if (newJsonStr.length() < jsonStreamEditorLength)
  1010. {
  1011. if (writeEnabled)
  1012. {
  1013. QByteArray jsonByteArray = newJsonStr.toUtf8();
  1014. while (jsonByteArray.length() != jsonStreamEditorLength)
  1015. {
  1016. jsonByteArray += '\x00';
  1017. }
  1018. if (lowRamMode) { rawPicContent = qUncompress(rawPicContent); }
  1019. QBuffer snapmaticStream(&rawPicContent);
  1020. snapmaticStream.open(QIODevice::ReadWrite);
  1021. if (!snapmaticStream.seek(jsonStreamEditorBegin))
  1022. {
  1023. snapmaticStream.close();
  1024. return false;
  1025. }
  1026. int result = snapmaticStream.write(jsonByteArray);
  1027. snapmaticStream.close();
  1028. if (result != 0)
  1029. {
  1030. jsonStr = newJsonStr;
  1031. if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
  1032. if (updateProperties) { parseJsonContent(); }
  1033. return true;
  1034. }
  1035. else
  1036. {
  1037. if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
  1038. return false;
  1039. }
  1040. }
  1041. else
  1042. {
  1043. return false;
  1044. }
  1045. }
  1046. return false;
  1047. }
  1048. // FILE MANAGEMENT
  1049. bool SnapmaticPicture::exportPicture(const QString &fileName, SnapmaticFormat format_)
  1050. {
  1051. // Keep current format when Auto_Format is used
  1052. SnapmaticFormat format = format_;
  1053. if (format_ == SnapmaticFormat::Auto_Format)
  1054. {
  1055. if (isCustomFormat)
  1056. {
  1057. format = SnapmaticFormat::G5E_Format;
  1058. }
  1059. else
  1060. {
  1061. format = SnapmaticFormat::PGTA_Format;
  1062. }
  1063. }
  1064. bool saveSuccess = false;
  1065. bool writeFailure = false;
  1066. #if QT_VERSION >= 0x050000
  1067. QSaveFile *picFile = new QSaveFile(fileName);
  1068. #else
  1069. QFile *picFile = new QFile(StandardPaths::tempLocation() % "/" % QFileInfo(fileName).fileName() % ".tmp");
  1070. #endif
  1071. if (picFile->open(QIODevice::WriteOnly))
  1072. {
  1073. if (format == SnapmaticFormat::G5E_Format)
  1074. {
  1075. // Modern compressed export (v2)
  1076. QByteArray g5eHeader;
  1077. g5eHeader.reserve(10);
  1078. g5eHeader += '\x00'; // First Null Byte
  1079. g5eHeader += QByteArray("G5E"); // GTA 5 Export
  1080. g5eHeader += '\x32'; g5eHeader += '\x00'; // 2 byte GTA 5 Export Version
  1081. g5eHeader += '\x00'; g5eHeader += '\x01'; // 2 byte GTA 5 Export Type
  1082. if (picFile->write(g5eHeader) == -1) { writeFailure = true; }
  1083. if (!lowRamMode)
  1084. {
  1085. if (picFile->write(qCompress(rawPicContent, 9)) == -1) { writeFailure = true; } // Compressed Snapmatic
  1086. }
  1087. else
  1088. {
  1089. if (picFile->write(rawPicContent) == -1) { writeFailure = true; }
  1090. }
  1091. #if QT_VERSION >= 0x050000
  1092. if (writeFailure) { picFile->cancelWriting(); }
  1093. else { saveSuccess = picFile->commit(); }
  1094. #else
  1095. if (!writeFailure) { saveSuccess = true; }
  1096. picFile->close();
  1097. #endif
  1098. delete picFile;
  1099. }
  1100. else if (format == SnapmaticFormat::JPEG_Format)
  1101. {
  1102. // JPEG export
  1103. QBuffer snapmaticStream(&rawPicContent);
  1104. snapmaticStream.open(QIODevice::ReadOnly);
  1105. if (snapmaticStream.seek(jpegStreamEditorBegin))
  1106. {
  1107. QByteArray jpegRawContent = snapmaticStream.read(jpegPicStreamLength);
  1108. if (jpegRawContentSizeE != 0)
  1109. {
  1110. jpegRawContent = jpegRawContent.left(jpegRawContentSizeE);
  1111. }
  1112. if (picFile->write(jpegRawContent) == -1) { writeFailure = true; }
  1113. #if QT_VERSION >= 0x050000
  1114. if (writeFailure) { picFile->cancelWriting(); }
  1115. else { saveSuccess = picFile->commit(); }
  1116. #else
  1117. if (!writeFailure) { saveSuccess = true; }
  1118. picFile->close();
  1119. #endif
  1120. }
  1121. delete picFile;
  1122. }
  1123. else
  1124. {
  1125. // Classic straight export
  1126. if (!lowRamMode)
  1127. {
  1128. if (picFile->write(rawPicContent) == -1) { writeFailure = true; }
  1129. }
  1130. else
  1131. {
  1132. if (picFile->write(qUncompress(rawPicContent)) == -1) { writeFailure = true; }
  1133. }
  1134. #if QT_VERSION >= 0x050000
  1135. if (writeFailure) { picFile->cancelWriting(); }
  1136. else { saveSuccess = picFile->commit(); }
  1137. #else
  1138. if (!writeFailure) { saveSuccess = true; }
  1139. picFile->close();
  1140. #endif
  1141. delete picFile;
  1142. }
  1143. #if QT_VERSION <= 0x050000
  1144. if (saveSuccess)
  1145. {
  1146. bool tempBakCreated = false;
  1147. if (QFile::exists(fileName))
  1148. {
  1149. if (!QFile::rename(fileName, fileName % ".tmp"))
  1150. {
  1151. QFile::remove(StandardPaths::tempLocation() % "/" % QFileInfo(fileName).fileName() % ".tmp");
  1152. return false;
  1153. }
  1154. tempBakCreated = true;
  1155. }
  1156. if (!QFile::rename(StandardPaths::tempLocation() % "/" % QFileInfo(fileName).fileName() % ".tmp", fileName))
  1157. {
  1158. QFile::remove(StandardPaths::tempLocation() % "/" % QFileInfo(fileName).fileName() % ".tmp");
  1159. if (tempBakCreated) { QFile::rename(fileName % ".tmp", fileName); }
  1160. return false;
  1161. }
  1162. if (tempBakCreated) { QFile::remove(fileName % ".tmp"); }
  1163. }
  1164. #endif
  1165. return saveSuccess;
  1166. }
  1167. else
  1168. {
  1169. delete picFile;
  1170. return saveSuccess;
  1171. }
  1172. }
  1173. void SnapmaticPicture::setPicFileName(const QString &picFileName_)
  1174. {
  1175. picFileName = picFileName_;
  1176. }
  1177. void SnapmaticPicture::setPicFilePath(const QString &picFilePath_)
  1178. {
  1179. picFilePath = picFilePath_;
  1180. }
  1181. bool SnapmaticPicture::deletePicFile()
  1182. {
  1183. if (!QFile::exists(picFilePath)) return true;
  1184. if (QFile::remove(picFilePath)) return true;
  1185. return false;
  1186. }
  1187. // VISIBILITY
  1188. bool SnapmaticPicture::isHidden()
  1189. {
  1190. if (picFilePath.right(7) == QLatin1String(".hidden"))
  1191. {
  1192. return true;
  1193. }
  1194. return false;
  1195. }
  1196. bool SnapmaticPicture::isVisible()
  1197. {
  1198. if (picFilePath.right(7) == QLatin1String(".hidden"))
  1199. {
  1200. return false;
  1201. }
  1202. return true;
  1203. }
  1204. bool SnapmaticPicture::setPictureHidden()
  1205. {
  1206. if (isCustomFormat)
  1207. {
  1208. return false;
  1209. }
  1210. if (!isHidden())
  1211. {
  1212. QString newPicFilePath = QString(picFilePath % ".hidden");
  1213. if (QFile::rename(picFilePath, newPicFilePath))
  1214. {
  1215. picFilePath = newPicFilePath;
  1216. return true;
  1217. }
  1218. return false;
  1219. }
  1220. return true;
  1221. }
  1222. bool SnapmaticPicture::setPictureVisible()
  1223. {
  1224. if (isCustomFormat)
  1225. {
  1226. return false;
  1227. }
  1228. if (isHidden())
  1229. {
  1230. QString newPicFilePath = QString(picFilePath).remove(picFilePath.length() - 7, 7);
  1231. if (QFile::rename(picFilePath, newPicFilePath))
  1232. {
  1233. picFilePath = newPicFilePath;
  1234. return true;
  1235. }
  1236. return false;
  1237. }
  1238. return true;
  1239. }
  1240. // PREDEFINED PROPERTIES
  1241. QSize SnapmaticPicture::getSnapmaticResolution()
  1242. {
  1243. return snapmaticResolution;
  1244. }
  1245. // SNAPMATIC DEFAULTS
  1246. bool SnapmaticPicture::isSnapmaticDefaultsEnforced()
  1247. {
  1248. return careSnapDefault;
  1249. }
  1250. void SnapmaticPicture::setSnapmaticDefaultsEnforced(bool enforced)
  1251. {
  1252. careSnapDefault = enforced;
  1253. }
  1254. // SNAPMATIC FORMAT
  1255. SnapmaticFormat SnapmaticPicture::getSnapmaticFormat()
  1256. {
  1257. if (isCustomFormat)
  1258. {
  1259. return SnapmaticFormat::G5E_Format;
  1260. }
  1261. return SnapmaticFormat::PGTA_Format;
  1262. }
  1263. void SnapmaticPicture::setSnapmaticFormat(SnapmaticFormat format)
  1264. {
  1265. if (format == SnapmaticFormat::G5E_Format)
  1266. {
  1267. isCustomFormat = true;
  1268. return;
  1269. }
  1270. else if (format == SnapmaticFormat::PGTA_Format)
  1271. {
  1272. isCustomFormat = false;
  1273. return;
  1274. }
  1275. qDebug() << "setSnapmaticFormat: Invalid SnapmaticFormat defined, valid SnapmaticFormats are G5E_Format and PGTA_Format";
  1276. }
  1277. bool SnapmaticPicture::isFormatSwitched()
  1278. {
  1279. return isFormatSwitch;
  1280. }
  1281. // VERIFY CONTENT
  1282. bool SnapmaticPicture::verifyTitle(const QString &title)
  1283. {
  1284. // VERIFY TITLE FOR BE A VALID SNAPMATIC TITLE
  1285. if (title.length() <= titlStreamCharacterMax && title.length() > 0)
  1286. {
  1287. for (const QChar &titleChar : title)
  1288. {
  1289. if (!verifyTitleChar(titleChar)) return false;
  1290. }
  1291. return true;
  1292. }
  1293. return false;
  1294. }
  1295. bool SnapmaticPicture::verifyTitleChar(const QChar &titleChar)
  1296. {
  1297. // VERIFY CHAR FOR BE A VALID SNAPMATIC CHARACTER
  1298. if (titleChar.isLetterOrNumber() || titleChar.isPrint())
  1299. {
  1300. if (titleChar == '<' || titleChar == '>' || titleChar == '\\') return false;
  1301. return true;
  1302. }
  1303. return false;
  1304. }
  1305. // STRING OPERATIONS
  1306. QString SnapmaticPicture::parseTitleString(const QByteArray &commitBytes, int maxLength)
  1307. {
  1308. Q_UNUSED(maxLength)
  1309. QString retStr = QTextCodec::codecForName("UTF-16LE")->toUnicode(commitBytes).trimmed();
  1310. retStr.remove(QChar('\x00'));
  1311. return retStr;
  1312. }
  1313. QString SnapmaticPicture::convertDrawStringForLog(const QString &inputStr)
  1314. {
  1315. QString outputStr = inputStr;
  1316. return outputStr.replace("&","&u;").replace(",", "&c;");
  1317. }
  1318. QString SnapmaticPicture::convertLogStringForDraw(const QString &inputStr)
  1319. {
  1320. QString outputStr = inputStr;
  1321. return outputStr.replace("&c;",",").replace("&u;", "&");
  1322. }