1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Merge pull request #1983 from profanity-im/feat/dl-location

Change default download location
This commit is contained in:
Michael Vetter 2024-06-20 13:52:51 +02:00 committed by GitHub
commit 4d9852a76a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 7 deletions

View File

@ -2572,7 +2572,7 @@ static const struct cmd_t command_defs[] = {
"Open or save URLs. This works with OMEMO encrypted files as well.") "Open or save URLs. This works with OMEMO encrypted files as well.")
CMD_ARGS( CMD_ARGS(
{ "open", "Open URL with predefined executable." }, { "open", "Open URL with predefined executable." },
{ "save", "Save URL to optional path, default path is current directory." }) { "save", "Save URL to optional path. The location is displayed after successful download." })
CMD_EXAMPLES( CMD_EXAMPLES(
"/url open https://profanity-im.github.io", "/url open https://profanity-im.github.io",
"/url save https://profanity-im.github.io/guide/latest/userguide.html /home/user/Download/") "/url save https://profanity-im.github.io/guide/latest/userguide.html /home/user/Download/")

View File

@ -9391,10 +9391,26 @@ cmd_slashguard(ProfWin* window, const char* const command, gchar** args)
} }
gchar* gchar*
_prepare_filename(gchar* url, gchar* path) _prepare_filename(ProfWin* window, gchar* url, gchar* path)
{ {
char* jid = NULL;
// lets skip private windows and put those files in general download folder
switch (window->type) {
case WIN_CHAT:
ProfChatWin* chatwin = (ProfChatWin*)window;
jid = chatwin->barejid;
break;
case WIN_MUC:
ProfMucWin* mucwin = (ProfMucWin*)window;
jid = mucwin->roomjid;
break;
default:
break;
}
// Ensure that the downloads directory exists for saving cleartexts. // Ensure that the downloads directory exists for saving cleartexts.
auto_gchar gchar* downloads_dir = path ? get_expanded_path(path) : files_get_data_path(DIR_DOWNLOADS); auto_gchar gchar* downloads_dir = path ? get_expanded_path(path) : files_get_download_path(jid);
if (g_mkdir_with_parents(downloads_dir, S_IRWXU) != 0) { if (g_mkdir_with_parents(downloads_dir, S_IRWXU) != 0) {
cons_show_error("Failed to create download directory " cons_show_error("Failed to create download directory "
"at '%s' with error '%s'", "at '%s' with error '%s'",
@ -9411,7 +9427,7 @@ _prepare_filename(gchar* url, gchar* path)
void void
_url_aesgcm_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* path) _url_aesgcm_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* path)
{ {
auto_gchar gchar* filename = _prepare_filename(url, path); auto_gchar gchar* filename = _prepare_filename(window, url, path);
if (!filename) if (!filename)
return; return;
auto_char char* id = get_random_string(4); auto_char char* id = get_random_string(4);
@ -9434,7 +9450,7 @@ _url_aesgcm_method(ProfWin* window, const char* cmd_template, gchar* url, gchar*
static gboolean static gboolean
_download_install_plugin(ProfWin* window, gchar* url, gchar* path) _download_install_plugin(ProfWin* window, gchar* url, gchar* path)
{ {
auto_gchar gchar* filename = _prepare_filename(url, path); auto_gchar gchar* filename = _prepare_filename(window, url, path);
if (!filename) if (!filename)
return FALSE; return FALSE;
HTTPDownload* download = malloc(sizeof(HTTPDownload)); HTTPDownload* download = malloc(sizeof(HTTPDownload));
@ -9452,7 +9468,7 @@ _download_install_plugin(ProfWin* window, gchar* url, gchar* path)
void void
_url_http_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* path) _url_http_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* path)
{ {
auto_gchar gchar* filename = _prepare_filename(url, path); auto_gchar gchar* filename = _prepare_filename(window, url, path);
if (!filename) if (!filename)
return; return;
auto_char char* id = get_random_string(4); auto_char char* id = get_random_string(4);
@ -9556,7 +9572,7 @@ cmd_url_save(ProfWin* window, const char* const command, gchar** args)
_url_aesgcm_method(window, cmd_template, url, path); _url_aesgcm_method(window, cmd_template, url, path);
#endif #endif
} else if (cmd_template != NULL) { } else if (cmd_template != NULL) {
auto_gchar gchar* filename = _prepare_filename(url, NULL); auto_gchar gchar* filename = _prepare_filename(window, url, NULL);
if (!filename) if (!filename)
return TRUE; return TRUE;
_url_external_method(cmd_template, url, filename); _url_external_method(cmd_template, url, filename);

View File

@ -164,6 +164,22 @@ files_get_data_path(const char* const location)
return g_strdup_printf("%s/profanity/%s", xdg_data, location); return g_strdup_printf("%s/profanity/%s", xdg_data, location);
} }
gchar*
files_get_download_path(const char* const jid)
{
auto_gchar gchar* xdg_data = _files_get_xdg_data_home();
if (jid) {
auto_char char* account_dir = str_replace(jid, "@", "_at_");
GDateTime* now = g_date_time_new_now_local();
auto_gchar gchar* date = g_date_time_format(now, "%Y_%m_%d");
g_date_time_unref(now);
return g_strdup_printf("%s/profanity/%s/%s/%s", xdg_data, DIR_DOWNLOADS, account_dir, date);
} else {
return g_strdup_printf("%s/profanity/%s", xdg_data, DIR_DOWNLOADS);
}
}
gchar* gchar*
files_get_account_data_path(const char* const specific_dir, const char* const jid) files_get_account_data_path(const char* const specific_dir, const char* const jid)
{ {

View File

@ -66,6 +66,7 @@ void files_create_directories(void);
gchar* files_get_config_path(const char* const config_base); gchar* files_get_config_path(const char* const config_base);
gchar* files_get_data_path(const char* const location); gchar* files_get_data_path(const char* const location);
gchar* files_get_download_path(const char* const jid);
gchar* files_get_account_data_path(const char* const specific_dir, const char* const jid); gchar* files_get_account_data_path(const char* const specific_dir, const char* const jid);
gchar* files_get_log_file(const char* const log_file); gchar* files_get_log_file(const char* const log_file);