-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
90 lines (73 loc) · 2.45 KB
/
MainWindow.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
#include "MainWindow.h"
#include "savefiledialog.hpp"
#include <giomm.h>
#include <glibmm.h>
#include <iostream>
#include <vector>
MainWindow::MainWindow()
: m_box{ Gtk::ORIENTATION_VERTICAL }
, m_saveFileButton{ "Save file" }
{
set_size_request(300, 300);
m_box.pack_start(m_saveFileButton, true, false);
add(m_box);
m_saveFileButton.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::onButtonClicked));
show_all();
}
void MainWindow::onButtonClicked()
{
SaveFileDialog dialog{ *this };
gint result = dialog.run();
if (result == GTK_RESPONSE_ACCEPT) {
std::string etag = "";
const std::string data = generateData();
Glib::RefPtr<Gio::File> file = dialog.get_file();
std::cout << "Target filepath: " << file->get_path() << std::endl;
try {
std::cout << "Saving using g_file_replace_contents" << std::endl;
file->replace_contents(data,
etag,
etag,
false,
Gio::FILE_CREATE_REPLACE_DESTINATION);
std::cout << "Success" << std::endl;
} catch (Glib::Error& e) {
std::cerr << e.what() << std::endl;
}
std::string tempFilePath;
int tempFileDescriptor = Glib::file_open_tmp(tempFilePath);
auto tempFile = Gio::File::create_for_path(tempFilePath);
try {
tempFile->replace_contents(data,
etag,
etag,
false,
Gio::FILE_CREATE_REPLACE_DESTINATION);
} catch (Glib::Error& e) {
std::cerr << e.what() << std::endl;
}
try {
std::cout << "Saving using g_file_copy" << std::endl;
tempFile->copy(file, Gio::FILE_COPY_OVERWRITE);
std::cout << "Success" << std::endl;
} catch (Glib::Error& e) {
std::cerr << e.what() << std::endl;
}
try {
std::cout << "Saving using g_file_move" << std::endl;
tempFile->move(file, Gio::FILE_COPY_OVERWRITE);
std::cout << "Success" << std::endl;
} catch (Glib::Error& e) {
std::cerr << e.what() << std::endl;
}
std::cout << "" << std::endl;
::close(tempFileDescriptor);
}
}
std::string MainWindow::generateData()
{
std::string result;
for (int i = 0; i < 40000; ++i)
result += "This is a test, you can safely delete me.\n";
return result;
}