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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#include <filebrowser_formaction.h>
#include <formatstring.h>
#include <logger.h>
#include <config.h>
#include <view.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/param.h>
#include <pwd.h>
#include <grp.h>
namespace newsbeuter {
filebrowser_formaction::filebrowser_formaction(view * vv, std::string formstr)
: formaction(vv,formstr), quit(false) { }
filebrowser_formaction::~filebrowser_formaction() { }
void filebrowser_formaction::process_operation(operation op, bool automatic, std::vector<std::string> * args) {
switch (op) {
case OP_OPEN:
{
/*
* whenever "ENTER" is hit, we need to distinguish two different cases:
* - the focus is in the list of files, then we need to set the filename field to the currently selected entry
* - the focus is in the filename field, then the filename needs to be returned.
*/
GetLogger().log(LOG_DEBUG,"filebrowser_formaction: 'opening' item");
std::string focus = f->get_focus();
if (focus.length() > 0) {
if (focus == "files") {
std::string selection = f->get("listposname");
char filetype = selection[0];
selection.erase(0,1);
std::string filename(selection);
switch (filetype) {
case 'd': {
std::string fileswidth = f->get("files:w");
std::istringstream is(fileswidth);
unsigned int width;
is >> width;
fmtstr_formatter fmt;
fmt.register_fmt('N', PROGRAM_NAME);
fmt.register_fmt('V', PROGRAM_VERSION);
// we use O only to distinguish between "open file" and "save file" for the %? format:
fmt.register_fmt('O', (type == FBT_OPEN) ? "dummy" : "");
fmt.register_fmt('f', filename);
f->set("head", fmt.do_format(v->get_cfg()->get_configvalue("filebrowser-title-format"), width));
}
::chdir(filename.c_str());
f->set("listpos","0");
if (type == FBT_SAVE) {
char cwdtmp[MAXPATHLEN];
::getcwd(cwdtmp,sizeof(cwdtmp));
std::string fn(cwdtmp);
fn.append(NEWSBEUTER_PATH_SEP);
std::string fnstr = f->get("filenametext");
const char * base = strrchr(fnstr.c_str(),'/');
if (!base)
base = fnstr.c_str();
fn.append(base);
f->set("filenametext",fn);
}
do_redraw = true;
break;
case '-':
{
char cwdtmp[MAXPATHLEN];
::getcwd(cwdtmp,sizeof(cwdtmp));
std::string fn(cwdtmp);
fn.append(NEWSBEUTER_PATH_SEP);
fn.append(filename);
f->set("filenametext",fn);
f->set_focus("filename");
}
break;
default:
// TODO: show error message
break;
}
} else {
bool do_pop = true;
std::string fn = f->get("filenametext");
struct stat sbuf;
/*
* this check is very important, as people will kill us if they accidentaly overwrote their files
* with no further warning...
*/
if (::stat(fn.c_str(), &sbuf)!=-1 && type == FBT_SAVE) {
char lbuf[2048];
snprintf(lbuf,sizeof(lbuf), _("Do you really want to overwrite `%s' (y:Yes n:No)? "), fn.c_str());
f->set_focus("files");
if (v->confirm(lbuf, _("yn")) == *_("n")) {
do_pop = false;
}
f->set_focus("filenametext");
}
if (do_pop)
v->pop_current_formaction();
}
}
}
break;
case OP_QUIT:
GetLogger().log(LOG_DEBUG,"view::filebrowser: quitting");
v->pop_current_formaction();
f->set("filenametext", "");
default:
break;
}
}
void filebrowser_formaction::prepare() {
/*
* prepare is always called before an operation is processed,
* and if a redraw is necessary, it updates the list of files
* in the current directory.
*/
if (do_redraw) {
char cwdtmp[MAXPATHLEN];
std::string code = "{list";
::getcwd(cwdtmp,sizeof(cwdtmp));
DIR * dirp = ::opendir(cwdtmp);
if (dirp) {
struct dirent * de = ::readdir(dirp);
while (de) {
if (strcmp(de->d_name,".")!=0)
code.append(add_file(de->d_name));
de = ::readdir(dirp);
}
::closedir(dirp);
}
code.append("}");
// GetLogger().log(LOG_DEBUG, "filebrowser: code = %s", code.c_str());
f->modify("files", "replace_inner", code);
do_redraw = false;
}
}
void filebrowser_formaction::init() {
char cwdtmp[MAXPATHLEN];
::getcwd(cwdtmp,sizeof(cwdtmp));
set_keymap_hints();
f->set("fileprompt", _("File: "));
if (dir == "") {
std::string save_path = v->get_cfg()->get_configvalue("save-path");
GetLogger().log(LOG_DEBUG,"view::filebrowser: save-path is '%s'",save_path.c_str());
dir = save_path;
}
GetLogger().log(LOG_DEBUG, "view::filebrowser: chdir(%s)", dir.c_str());
::chdir(dir.c_str());
::getcwd(cwdtmp,sizeof(cwdtmp));
f->set("filenametext", default_filename);
char buf[1024];
if (type == FBT_OPEN) {
snprintf(buf, sizeof(buf), _("%s %s - Open File - %s"), PROGRAM_NAME, PROGRAM_VERSION, cwdtmp);
} else {
snprintf(buf, sizeof(buf), _("%s %s - Save File - %s"), PROGRAM_NAME, PROGRAM_VERSION, cwdtmp);
}
f->set("head", buf);
}
keymap_hint_entry * filebrowser_formaction::get_keymap_hint() {
static keymap_hint_entry hints[] = {
{ OP_QUIT, _("Cancel") },
{ OP_OPEN, _("Save") },
{ OP_NIL, NULL }
};
return hints;
}
std::string filebrowser_formaction::add_file(std::string filename) {
std::string retval;
struct stat sb;
if (::stat(filename.c_str(),&sb)==0) {
char ftype = '?';
if (sb.st_mode & S_IFREG)
ftype = '-';
else if (sb.st_mode & S_IFDIR)
ftype = 'd';
else if (sb.st_mode & S_IFBLK)
ftype = 'b';
else if (sb.st_mode & S_IFCHR)
ftype = 'c';
else if (sb.st_mode & S_IFIFO)
ftype = 'p';
else if (sb.st_mode & S_IFLNK)
ftype = 'l';
std::string rwxbits = get_rwx(sb.st_mode & 0777);
std::string owner = "????????", group = "????????";
struct passwd * spw = getpwuid(sb.st_uid);
if (spw) {
owner = spw->pw_name;
for (int i=owner.length();i<8;++i) {
owner.append(" ");
}
}
struct group * sgr = getgrgid(sb.st_gid);
if (sgr) {
group = sgr->gr_name;
for (int i=group.length();i<8;++i) {
group.append(" ");
}
}
std::ostringstream os;
os << std::setw(12) << sb.st_size;
std::string sizestr = os.str();
std::string line;
line.append(1,ftype);
line.append(rwxbits);
line.append(" ");
line.append(owner);
line.append(" ");
line.append(group);
line.append(" ");
line.append(sizestr);
line.append(" ");
line.append(filename);
retval = "{listitem[";
retval.append(1,ftype);
retval.append(stfl::quote(filename));
retval.append("] text:");
retval.append(stfl::quote(line));
retval.append("}");
}
return retval;
}
std::string filebrowser_formaction::get_rwx(unsigned short val) {
std::string str;
const char * bitstrs[] = { "---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx" };
for (int i=0;i<3;++i) {
unsigned char bits = val % 8;
val /= 8;
str.insert(0, bitstrs[bits]);
}
return str;
}
}
|