Skip to content

Commit f5a8327

Browse files
committed
Add a symlink option
1 parent 04ef193 commit f5a8327

File tree

3 files changed

+105
-58
lines changed

3 files changed

+105
-58
lines changed

commander.cpp

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <functional>
12
#include <iostream>
23
#include <sstream>
34
#include "commander.h"
@@ -203,32 +204,70 @@ const bool CCommander::keyHold(void)
203204

204205
const bool CCommander::openCopyMenu(void) const
205206
{
206-
bool l_ret(false);
207+
std::vector<std::function<bool()>> handlers;
207208
int l_dialogRetVal(0);
208-
bool l_rename(false);
209209
// List of selected files
210210
std::vector<std::string> l_list;
211211
m_panelSource->getSelectList(l_list);
212-
// The rename option appears only if one item is selected
213-
l_rename = (l_list.size() == 1);
214212
{
215213
bool l_loop(false);
216214
std::ostringstream l_stream;
217215
l_stream << l_list.size() << " selected:";
218216
// File operation dialog
219217
CDialog l_dialog(l_stream.str(), 0, Y_LIST + m_panelSource->getHighlightedIndexRelative() * LINE_HEIGHT);
218+
220219
l_dialog.addOption(m_panelSource == &m_panelLeft ? "Copy >" : "< Copy");
220+
handlers.push_back([&]() {
221+
File_utils::copyFile(l_list, m_panelTarget->getCurrentPath());
222+
return true;
223+
});
224+
221225
l_dialog.addOption(m_panelSource == &m_panelLeft ? "Move >" : "< Move");
222-
if (l_rename)
226+
handlers.push_back([&]() {
227+
File_utils::moveFile(l_list, m_panelTarget->getCurrentPath());
228+
return true;
229+
});
230+
231+
232+
l_dialog.addOption(m_panelSource == &m_panelLeft ? "Symlink >" : "< Symlink");
233+
handlers.push_back([&]() {
234+
File_utils::symlinkFile(l_list, m_panelTarget->getCurrentPath());
235+
return true;
236+
});
237+
238+
if (l_list.size() == 1) {
239+
// The rename option appears only if one item is selected
223240
l_dialog.addOption("Rename");
241+
handlers.push_back([&]() {
242+
CKeyboard l_keyboard(m_panelSource->getHighlightedItem());
243+
if (l_keyboard.execute() == 1 && !l_keyboard.getInputText().empty() && l_keyboard.getInputText() != m_panelSource->getHighlightedItem())
244+
{
245+
File_utils::renameFile(m_panelSource->getHighlightedItemFull(), m_panelSource->getCurrentPath() + (m_panelSource->getCurrentPath() == "/" ? "" : "/") + l_keyboard.getInputText());
246+
return true;
247+
}
248+
return false;
249+
});
250+
}
251+
224252
l_dialog.addOption("Delete");
253+
handlers.push_back([&]() {
254+
File_utils::removeFile(l_list);
255+
return true;
256+
});
257+
const int delete_option = handlers.size();
258+
225259
l_dialog.addOption("Disk used");
260+
handlers.push_back([&]() {
261+
File_utils::diskUsed(l_list);
262+
return false;
263+
});
264+
226265
l_dialog.init();
227266
do
228267
{
229268
l_loop = false;
230269
l_dialogRetVal = l_dialog.execute();
231-
if (l_dialogRetVal == 3 + l_rename)
270+
if (l_dialogRetVal == delete_option)
232271
{
233272
CDialog l_dialog2("", l_dialog.getX() + l_dialog.getImage()->w / screen.ppu_x - DIALOG_BORDER, l_dialog.getY() / screen.ppu_y + DIALOG_BORDER + (l_dialog.getHighlightedIndex() + 1) * LINE_HEIGHT);
234273
l_dialog2.addOption("Yes");
@@ -241,59 +280,10 @@ const bool CCommander::openCopyMenu(void) const
241280
while (l_loop);
242281
}
243282
// Perform operation
244-
switch (l_dialogRetVal)
245-
{
246-
case 1:
247-
// Copy
248-
File_utils::copyFile(l_list, m_panelTarget->getCurrentPath());
249-
l_ret = true;
250-
break;
251-
case 2:
252-
// Move
253-
File_utils::moveFile(l_list, m_panelTarget->getCurrentPath());
254-
l_ret = true;
255-
break;
256-
case 3:
257-
if (l_rename)
258-
{
259-
// Rename
260-
CKeyboard l_keyboard(m_panelSource->getHighlightedItem());
261-
if (l_keyboard.execute() == 1 && !l_keyboard.getInputText().empty() && l_keyboard.getInputText() != m_panelSource->getHighlightedItem())
262-
{
263-
File_utils::renameFile(m_panelSource->getHighlightedItemFull(), m_panelSource->getCurrentPath() + (m_panelSource->getCurrentPath() == "/" ? "" : "/") + l_keyboard.getInputText());
264-
l_ret = true;
265-
}
266-
}
267-
else
268-
{
269-
// Delete
270-
printf("About to remove file");
271-
printf("%s", l_list.at(0).c_str());
272-
File_utils::removeFile(l_list);
273-
printf("Done removing file.");
274-
l_ret = true;
275-
}
276-
break;
277-
case 4:
278-
if (l_rename)
279-
{
280-
// Delete
281-
File_utils::removeFile(l_list);
282-
l_ret = true;
283-
}
284-
else
285-
// Disk used
286-
File_utils::diskUsed(l_list);
287-
break;
288-
case 5:
289-
if (l_rename)
290-
// Disk used
291-
File_utils::diskUsed(l_list);
292-
break;
293-
default:
294-
break;
283+
if (l_dialogRetVal > 0 && l_dialogRetVal <= handlers.size()) {
284+
return handlers[l_dialogRetVal - 1]();
295285
}
296-
return l_ret;
286+
return false;
297287
}
298288

299289
const bool CCommander::openSystemMenu(void)

fileutils.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,61 @@ void File_utils::moveFile(const std::vector<std::string> &p_src, const std::stri
206206
}
207207
}
208208

209+
void File_utils::symlinkFile(const std::vector<std::string> &p_src, const std::string &p_dest)
210+
{
211+
std::string l_destFile;
212+
std::string l_fileName;
213+
bool l_loop(true);
214+
bool l_confirm(true);
215+
bool l_execute(true);
216+
for (std::vector<std::string>::const_iterator l_it = p_src.begin(); l_loop && (l_it != p_src.end()); ++l_it)
217+
{
218+
l_execute = true;
219+
l_fileName = getFileName(*l_it);
220+
l_destFile = p_dest + (p_dest.at(p_dest.size() - 1) == '/' ? "" : "/") + l_fileName;
221+
222+
// Check if destination files already exists
223+
if (l_confirm)
224+
{
225+
if (fileExists(l_destFile))
226+
{
227+
INHIBIT(std::cout << "File " << l_destFile << " already exists => ask for confirmation" << std::endl;)
228+
CDialog l_dialog("Question:", 0, 0);
229+
l_dialog.addLabel("Overwrite " + l_fileName + "?");
230+
l_dialog.addOption("Yes");
231+
l_dialog.addOption("Yes to all");
232+
l_dialog.addOption("No");
233+
l_dialog.addOption("Cancel");
234+
l_dialog.init();
235+
switch (l_dialog.execute())
236+
{
237+
case 1:
238+
// Yes
239+
break;
240+
case 2:
241+
// Yes to all
242+
l_confirm = false;
243+
break;
244+
case 3:
245+
// No
246+
l_execute = false;
247+
break;
248+
default:
249+
// Cancel
250+
l_execute = false;
251+
l_loop = false;
252+
break;
253+
}
254+
}
255+
}
256+
if (l_execute)
257+
{
258+
Run("ln", "-sf", *l_it, p_dest);
259+
Run("sync", l_destFile);
260+
}
261+
}
262+
}
263+
209264
void File_utils::renameFile(const std::string &p_file1, const std::string &p_file2)
210265
{
211266
bool l_execute(true);

fileutils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace File_utils
1212

1313
void moveFile(const std::vector<std::string> &p_src, const std::string &p_dest);
1414

15+
void symlinkFile(const std::vector<std::string> &p_src, const std::string &p_dest);
16+
1517
void removeFile(const std::vector<std::string> &p_files);
1618

1719
void executeFile(const std::string &p_file);

0 commit comments

Comments
 (0)