| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <QtGui/QtGui> |
| 22 | #include "ImgurResponseLogger.h" |
| 23 | |
| 24 | ImgurResponseLogger::ImgurResponseLogger() |
| 25 | { |
| 26 | mLogFilename = QLatin1String("imgur_history.txt"); |
| 27 | mLogPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); |
| 28 | mLogFilePath = mLogPath + QLatin1String("/") + mLogFilename; |
| 29 | } |
| 30 | |
| 31 | void ImgurResponseLogger::log(const ImgurResponse &response) |
| 32 | { |
| 33 | createPathIfRequired(); |
| 34 | auto logEntry = getLogEntry(response); |
| 35 | |
| 36 | writeLogEntry(logEntry); |
| 37 | } |
| 38 | |
| 39 | void ImgurResponseLogger::writeLogEntry(const QString &logEntry) const |
| 40 | { |
| 41 | QFile file(mLogFilePath); |
| 42 | auto fileOpened = file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text); |
| 43 | if(fileOpened) { |
| 44 | QTextStream stream(&file); |
| 45 | stream << logEntry << Qt::endl; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void ImgurResponseLogger::createPathIfRequired() const |
| 50 | { |
| 51 | QDir qdir; |
| 52 | qdir.mkpath(mLogPath); |
| 53 | } |
| 54 | |
| 55 | QString ImgurResponseLogger::getLogEntry(const ImgurResponse &response) const |
| 56 | { |
| 57 | auto separator = QLatin1String(","); |
| 58 | auto deleteLink = QLatin1String("https://imgur.com/delete/") + response.deleteHash(); |
| 59 | auto timestamp = response.timeStamp().toString(QLatin1String("dd.MM.yyyy hh:mm:ss")); |
| 60 | return timestamp + separator + response.link() + separator + deleteLink; |
| 61 | } |
| 62 | |
| 63 | QStringList ImgurResponseLogger::getLogs() const |
| 64 | { |
| 65 | auto logEntries = QStringList(); |
| 66 | QFile file(mLogFilePath); |
| 67 | auto fileOpened = file.open(QIODevice::ReadOnly); |
| 68 | if (fileOpened) { |
| 69 | |
| 70 | QTextStream stream(&file); |
| 71 | while (!stream.atEnd()) { |
| 72 | auto entry = stream.readLine(); |
| 73 | logEntries.append(entry); |
| 74 | } |
| 75 | } |
| 76 | return logEntries; |
| 77 | } |