-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioop.cpp
110 lines (100 loc) · 2.6 KB
/
ioop.cpp
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
#include "ioop.h"
#include <QUrl>
#include <QMessageBox>
#include <QFile>
#include <DNotifySender>
#include <QFileDialog>
#include <QDebug>
using Sender = Dtk::Core::DUtil::DNotifySender;
IOOP::IOOP(QObject *parent) : QObject(parent)
{
box = new QMessageBox;
}
IOOP::~IOOP()
{
box->~QMessageBox();
path.~QUrl();
data.~QString();
if (file != nullptr) {
file->~QFile();
}
}
void IOOP::writeToFile(QUrl p, QString d)
{
path = p;
data = d;
if (file != nullptr) {
file->~QFile();
}
file = new QFile(path.path());
opened = file->open(QIODevice::OpenModeFlag::NewOnly);
if (opened != false) {
continueOW();
}
}
void IOOP::continueOW()
{
if (opened == false) {
opened = file->open(QIODevice::OpenModeFlag::Truncate);
}
if (opened == true) {
file->write(data.toLatin1());
file->flush();
} else {
Sender send("Failed writting to file");
send.timeOut(3000);
send.appIcon("preferences-system");
send.appName("TLA");
send.appBody("Error: " + file->errorString() + "\nFile: \"" + path.url() + "\"");
send.call();
send.~Sender();
}
clearIn();
}
void IOOP::clearIn()
{
if (file != nullptr) {
file->close();
file->~QFile();
}
data.clear();
path.clear();
}
void IOOP::openFile(QUrl p)
{
file = new QFile(p.path());
bool failed = true;
if (file->open(QIODevice::OpenModeFlag::ReadOnly)) {
if (file->size() > 0) {
Q_EMIT fileDataRead(file->readAll());
failed = false;
}
}
if (failed == true) {
Sender send("Failed to open file");
send.timeOut(3000);
send.appIcon("preferences-system");
send.appName("TLA");
send.appBody("Error: " + file->errorString() + "\nFile:\"" + p.url() + "\"");
send.call();
send.~Sender();
}
clearIn();
}
void IOOP::uiOpenFile()
{
QString file = QFileDialog::getOpenFileName(nullptr, tr("Open a file"),
"~/Documents", tr("Web pages (*.html)\n"
"Custom Data Type (*.cdt)\n"
"MarkDown (*.md)\n"));
if (!file.isEmpty()) {
openFile(QUrl(QString("file://%1").arg(file)));
}
}
void IOOP::uiSaveFile(QString data)
{
QString file = QFileDialog::getSaveFileName(nullptr, tr("Select a file"), "~/Documents", tr("Custom Data Type (*.cdt)\n"));
if (!file.isEmpty()) {
writeToFile(QUrl(QString("file://%1").arg(file)), data);
}
}