Skip to content

Commit

Permalink
Add 'Copy to location...' option for copying files
Browse files Browse the repository at this point in the history
refs #3190
refs #1929
refs #1862
  • Loading branch information
andoma committed Jul 19, 2016
1 parent 7a1194b commit 1b56ee1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
7 changes: 6 additions & 1 deletion glwskins/flat/ctxmenu/default_details.view
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ widget(popup, {
"skin:https://icons/ic_check_48px.svg",
""));

ITEM(_("Copy"), {
ITEM(_("Copy to clipboard"), {
fireEvent(deliverRef($core.clipboard.setFromItem, $args));
$clone.itemMenu = false;
}, void, !$args.canCopy);

ITEM(_("Copy to location..."), {
fireEvent(deliverRef($core.clipboard.copyItem, $args));
$clone.itemMenu = false;
}, void, !$args.canCopy);

ITEM(_("Delete"), {
delete($args);
$clone.itemMenu = void;
Expand Down
2 changes: 1 addition & 1 deletion glwskins/flat/popups/filepicker.view
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ popup({
label({
hidden: !$self.dirmode;
align: center;
caption: _("Folder mode, press right to open folder");
caption: _("Folder select mode, press right to open subfolder");
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/fileaccess/fa_filepicker.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ eventsink(void *opaque, prop_event_t event, ...)
/**
*
*/
static rstr_t *
rstr_t *
filepicker_pick(const char *title, int flags)
{
filepicker_t fp = {0};
Expand Down
3 changes: 3 additions & 0 deletions src/fileaccess/fa_filepicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@

void filepicker_pick_to_prop(const char *title, prop_t *target,
const char *current, int flags);

rstr_t *filepicker_pick(const char *title, int flags);

83 changes: 59 additions & 24 deletions src/ui/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "prop/prop.h"
#include "clipboard.h"
#include "fileaccess/fileaccess.h"
#include "fileaccess/fa_filepicker.h"
#include "task.h"
#include "notifications.h"

static hts_mutex_t clipboard_mutex;
static rstr_t *clipboard_content;
Expand All @@ -14,19 +16,19 @@ static rstr_t *clipboard_content;
static void
clipboard_setFromItem(void *opaque, event_t *e)
{
if(event_is_type(e, EVENT_PROPREF)) {
event_prop_t *ep = (event_prop_t *)e;
rstr_t *url = prop_get_string(ep->p, "url", NULL);
if(gconf.clipboard_set) {
gconf.clipboard_set(rstr_get(url));
} else {
hts_mutex_lock(&clipboard_mutex);
rstr_set(&clipboard_content, url);
hts_mutex_unlock(&clipboard_mutex);
}
rstr_release(url);
clipboard_validate_contents();
if(!event_is_type(e, EVENT_PROPREF))
return;
event_prop_t *ep = (event_prop_t *)e;
rstr_t *url = prop_get_string(ep->p, "url", NULL);
if(gconf.clipboard_set) {
gconf.clipboard_set(rstr_get(url));
} else {
hts_mutex_lock(&clipboard_mutex);
rstr_set(&clipboard_content, url);
hts_mutex_unlock(&clipboard_mutex);
}
rstr_release(url);
clipboard_validate_contents();
}


Expand All @@ -41,12 +43,14 @@ typedef struct clipboard_copy_job {
prop_t *completed_prop;
prop_t *files_prop;

char errbuf[256];

} clipboard_copy_job_t;

static int
clipboard_copy_file(const char *src, const char *dst, clipboard_copy_job_t *j)
{
fa_handle_t *sfh = fa_open(src, NULL, 0);
fa_handle_t *sfh = fa_open(src, j->errbuf, sizeof(j->errbuf));
if(sfh == NULL)
return 1;

Expand All @@ -55,8 +59,9 @@ clipboard_copy_file(const char *src, const char *dst, clipboard_copy_job_t *j)
fa_url_get_last_component(filename, sizeof(filename), src);
fa_pathjoin(dstpath, sizeof(dstpath), dst, filename);

fa_handle_t *dfh = fa_open_ex(dstpath, NULL, 0, FA_WRITE, NULL);
if(dst == NULL) {
fa_handle_t *dfh = fa_open_ex(dstpath, j->errbuf, sizeof(j->errbuf),
FA_WRITE, NULL);
if(dfh == NULL) {
fa_close(sfh);
return 1;
}
Expand All @@ -69,6 +74,7 @@ clipboard_copy_file(const char *src, const char *dst, clipboard_copy_job_t *j)

if(fa_write(dfh, buf, r) != r) {
rcode = 1;
snprintf(j->errbuf, sizeof(j->errbuf), "Write failure");
break;
}
j->completed += r;
Expand All @@ -89,7 +95,7 @@ clipboard_copy_files0(const char *src, const char *dst, clipboard_copy_job_t *j)
fa_stat_t st;
char dstpath[1024];

if(fa_stat(src, &st, NULL, 0))
if(fa_stat(src, &st, j->errbuf, sizeof(j->errbuf)))
return 1;

if(st.fs_type == CONTENT_FILE) {
Expand All @@ -113,7 +119,7 @@ clipboard_copy_files0(const char *src, const char *dst, clipboard_copy_job_t *j)
return 1;
}

fa_dir_t *fd = fa_scandir(src, NULL, 0);
fa_dir_t *fd = fa_scandir(src, j->errbuf, sizeof(j->errbuf));
if(fd == NULL)
return 1;
int rcode = 0;
Expand Down Expand Up @@ -151,8 +157,7 @@ static void
clipboard_copy_files(const char *src, const char *dst)
{
clipboard_copy_job_t j = {};
TRACE(TRACE_DEBUG, "COPY", "Copy from %s to %s", src, dst);

TRACE(TRACE_DEBUG, "COPY", "Copy from %s to directory %s", src, dst);

prop_t *p = prop_create(prop_create(prop_get_global(), "clipboard"),
"copyprogress");
Expand All @@ -164,13 +169,18 @@ clipboard_copy_files(const char *src, const char *dst)
j.completed_prop = prop_create(n, "completed");
j.files_prop = prop_create(n, "files");

if(clipboard_copy_files0(src, NULL, &j))
if(clipboard_copy_files0(src, NULL, &j)) {
notify_add(NULL, NOTIFY_ERROR, NULL, 5,
_("Copy failed: %s"), j.errbuf);
return;

}
TRACE(TRACE_DEBUG, "COPY", "Copy from %s total %"PRId64" bytes",
src, j.total_bytes);

clipboard_copy_files0(src, dst, &j);
if(clipboard_copy_files0(src, dst, &j)) {
notify_add(NULL, NOTIFY_ERROR, NULL, 5,
_("Copy failed: %s"), j.errbuf);
}
prop_destroy(n);
}

Expand Down Expand Up @@ -244,21 +254,46 @@ clipboard_get(void)
}


/**
*
*/
static void
clipboard_copyItem(void *opaque, event_t *e)
{
if(!event_is_type(e, EVENT_PROPREF))
return;
event_prop_t *ep = (event_prop_t *)e;
rstr_t *src = prop_get_string(ep->p, "url", NULL);

rstr_t *title = _("Target folder");
rstr_t *dst = filepicker_pick(rstr_get(title), FILEPICKER_DIRECTORIES);
rstr_release(title);
if(dst != NULL) {
clipboard_copy_files(rstr_get(src), rstr_get(dst));
}
rstr_release(src);
}



/**
*
*/
static void
clipboard_init(void)
{
hts_mutex_init(&clipboard_mutex);



prop_subscribe(0,
PROP_TAG_CALLBACK_EVENT, clipboard_setFromItem, NULL,
PROP_TAG_NAME("global", "clipboard", "setFromItem"),
NULL);

prop_subscribe(0,
PROP_TAG_CALLBACK_EVENT, clipboard_copyItem, NULL,
PROP_TAG_NAME("global", "clipboard", "copyItem"),
NULL);

prop_subscribe(0,
PROP_TAG_CALLBACK_EVENT, clipboard_pasteToModel, NULL,
PROP_TAG_NAME("global", "clipboard", "pasteToModel"),
Expand Down

0 comments on commit 1b56ee1

Please sign in to comment.