forked from mika314/texteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_dialog.cpp
92 lines (85 loc) · 2.45 KB
/
save_dialog.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
#include "save_dialog.hpp"
#include "to_utf16.hpp"
#include "to_utf8.hpp"
#include "text_file.hpp"
#include "screen.hpp"
#include <sys/types.h>
#include <dirent.h>
#include <iostream>
#include <algorithm>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/param.h>
SaveDialog::SaveDialog(Screen *screen, TextFile *textFile):
screen_(screen),
textFile_(textFile)
{
scanDirectory();
}
std::wstring SaveDialog::preInsert(Coord &cursor, std::wstring value)
{
if (cursor.y == 1 && value.find(L"\n") == std::wstring::npos)
return value;
else if (value.find(L"\n") != std::wstring::npos && cursor.y > 0)
{
struct stat buf;
auto fileName = toUtf8(buffer_[screen_->cursor().y]);
stat(fileName.c_str(), &buf);
if (S_ISDIR(buf.st_mode))
{
auto folderName = buffer_[0];
folderName = { begin(folderName) + folderName.rfind(L"/") + 1, end(folderName) - 1 };
chdir(fileName.c_str());
scanDirectory();
int line = -1;
for (size_t i = 0; i < buffer_.size(); ++i)
if (buffer_[i] == folderName)
{
line = i;
break;
}
if (line != -1)
cursor = { 0, line };
else
cursor = { 0, 1 };
}
else
saveAs.emit(this, textFile_, fileName);
}
return L"";
}
int SaveDialog::preDelete(const Coord cursor, int value)
{
if (cursor.y == 1 && cursor.x <= static_cast<int>(buffer_[screen_->cursor().y].size()) - value)
return value;
else
return 0;
}
int SaveDialog::preBackspace(Coord &cursor, int value)
{
if (cursor.y == 1 && cursor.x >= value)
return value;
else
return 0;
}
void SaveDialog::scanDirectory()
{
buffer_.clear();
char *tmp = getcwd(nullptr, MAXPATHLEN);
auto currentDir = toUtf16(tmp);
free(tmp);
setName(std::wstring{begin(currentDir) + currentDir.rfind(L'/'), end(currentDir)});
buffer_.push_back(currentDir + L":");
auto d = opendir(".");
buffer_.push_back(L"");
while (auto de = readdir(d))
{
auto fileName = toUtf16(de->d_name);
if (fileName == L".." ||
(fileName.size() > 0 && fileName[0] != L'.' && fileName[fileName.size() - 1] != L'~'))
buffer_.push_back(fileName);
}
closedir(d);
std::sort(begin(buffer_) + 2, end(buffer_));
}