2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-07-22 15:55:48 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2022-04-25 01:49:52 +00:00
|
|
|
#include <cstddef>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <ctime>
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <functional>
|
2022-08-22 02:26:13 +00:00
|
|
|
#include <list>
|
2021-12-14 08:43:27 +00:00
|
|
|
#include <string>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <vector>
|
2021-12-14 08:43:27 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <sys/types.h>
|
2021-11-01 00:11:23 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <glibmm.h>
|
|
|
|
#include <gtkmm.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2021-08-15 09:41:48 +00:00
|
|
|
#include <libtransmission/tr-macros.h>
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2022-08-22 02:26:13 +00:00
|
|
|
#include "Session.h"
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
extern int const mem_K;
|
2021-12-14 08:43:27 +00:00
|
|
|
extern char const* const mem_K_str;
|
|
|
|
extern char const* const mem_M_str;
|
|
|
|
extern char const* const mem_G_str;
|
|
|
|
extern char const* const mem_T_str;
|
2017-04-20 16:02:19 +00:00
|
|
|
|
|
|
|
extern int const disk_K;
|
2021-12-14 08:43:27 +00:00
|
|
|
extern char const* const disk_K_str;
|
|
|
|
extern char const* const disk_M_str;
|
|
|
|
extern char const* const disk_G_str;
|
|
|
|
extern char const* const disk_T_str;
|
2017-04-20 16:02:19 +00:00
|
|
|
|
|
|
|
extern int const speed_K;
|
2021-12-14 08:43:27 +00:00
|
|
|
extern char const* const speed_K_str;
|
|
|
|
extern char const* const speed_M_str;
|
|
|
|
extern char const* const speed_G_str;
|
|
|
|
extern char const* const speed_T_str;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
enum class GtrUnicode
|
2010-03-20 14:53:08 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
Up,
|
|
|
|
Down,
|
|
|
|
Inf,
|
|
|
|
Bullet
|
2010-03-20 14:53:08 +00:00
|
|
|
};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
Glib::ustring gtr_get_unicode_string(GtrUnicode);
|
2010-03-20 14:53:08 +00:00
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
/* return a human-readable string for the size given in bytes. */
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring tr_strlsize(guint64 size);
|
2007-06-06 00:30:13 +00:00
|
|
|
|
2008-01-04 18:52:39 +00:00
|
|
|
/* return a human-readable string for the given ratio. */
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring tr_strlratio(double ratio);
|
2008-01-04 18:52:39 +00:00
|
|
|
|
2022-06-15 02:56:27 +00:00
|
|
|
std::string tr_format_time_relative(time_t src, time_t tgt);
|
|
|
|
std::string tr_format_time_left(time_t seconds);
|
|
|
|
std::string tr_format_time(time_t seconds);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-07-22 15:55:48 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_open_uri(Glib::ustring const& uri);
|
2010-09-22 16:44:38 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_open_file(std::string const& path);
|
2008-04-11 02:21:33 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring gtr_get_help_uri();
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2009-07-22 15:55:48 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* backwards-compatible wrapper around gtk_widget_set_visible() */
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_widget_set_visible(Gtk::Widget&, bool);
|
2010-07-24 03:07:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_dialog_set_content(Gtk::Dialog& dialog, Gtk::Widget& content);
|
2010-12-22 03:12:47 +00:00
|
|
|
|
2010-02-02 07:51:45 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::ComboBox* gtr_priority_combo_new();
|
2017-04-19 12:04:45 +00:00
|
|
|
#define gtr_priority_combo_get_value(w) gtr_combo_box_get_active_enum(w)
|
2017-04-21 07:40:57 +00:00
|
|
|
#define gtr_priority_combo_set_value(w, val) gtr_combo_box_set_active_enum(w, val)
|
2010-07-23 06:54:49 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::ComboBox* gtr_combo_box_new_enum(std::vector<std::pair<Glib::ustring, int>> const& items);
|
|
|
|
int gtr_combo_box_get_active_enum(Gtk::ComboBox const&);
|
|
|
|
void gtr_combo_box_set_active_enum(Gtk::ComboBox&, int value);
|
2010-02-02 07:51:45 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_unrecognized_url_dialog(Gtk::Widget& parent, Glib::ustring const& url);
|
2010-02-05 13:59:44 +00:00
|
|
|
|
2021-12-24 22:05:17 +00:00
|
|
|
void gtr_add_torrent_error_dialog(Gtk::Widget& window_or_child, tr_torrent* duplicate_torrent, std::string const& filename);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/* pop up the context menu if a user right-clicks.
|
|
|
|
if the row they right-click on isn't selected, select it. */
|
2021-10-18 20:22:31 +00:00
|
|
|
bool on_tree_view_button_pressed(
|
|
|
|
Gtk::TreeView* view,
|
|
|
|
GdkEventButton* event,
|
|
|
|
std::function<void(GdkEventButton*)> const& callback = {});
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-09-27 18:01:31 +00:00
|
|
|
/* if the click didn't specify a row, clear the selection */
|
2021-10-18 20:22:31 +00:00
|
|
|
bool on_tree_view_button_released(Gtk::TreeView* view, GdkEventButton* event);
|
2008-01-15 17:55:09 +00:00
|
|
|
|
2009-07-22 15:55:48 +00:00
|
|
|
/* move a file to the trashcan if GIO is available; otherwise, delete it */
|
2021-10-18 20:22:31 +00:00
|
|
|
bool gtr_file_trash_or_remove(std::string const& filename, tr_error** error);
|
2008-03-09 15:27:08 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_paste_clipboard_url_into_entry(Gtk::Entry& entry);
|
2011-01-14 21:57:20 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* Only call gtk_label_set_text() if the new text differs from the old.
|
2011-01-21 16:32:27 +00:00
|
|
|
* This prevents the label from having to recalculate its size
|
|
|
|
* and prevents selected text in the label from being deselected */
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_label_set_text(Gtk::Label& lb, Glib::ustring const& text);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T gtr_str_strip(T const& text)
|
|
|
|
{
|
|
|
|
auto const new_begin = text.find_first_not_of("\t\n\v\f\r ");
|
|
|
|
auto const new_end = text.find_last_not_of("\t\n\v\f\r ");
|
2021-10-28 20:52:25 +00:00
|
|
|
return new_begin == T::npos ? T() : text.substr(new_begin, new_end == T::npos ? new_end : new_end - new_begin + 1);
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
std::string gtr_get_full_resource_path(std::string const& rel_path);
|
|
|
|
|
2022-08-22 02:26:13 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
extern size_t const max_recent_dirs;
|
|
|
|
std::list<std::string> gtr_get_recent_dirs(std::string const& pref);
|
|
|
|
void gtr_save_recent_dir(std::string const& pref, Glib::RefPtr<Session> const& core, std::string const& dir);
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace gtr_detail
|
|
|
|
{
|
|
|
|
|
|
|
|
#if G_ENCODE_VERSION(GLIBMM_MAJOR_VERSION, GLIBMM_MINOR_VERSION) < G_ENCODE_VERSION(2, 62)
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T const& sprintify(T const& arg)
|
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline char const* sprintify(Glib::ustring const& arg)
|
|
|
|
{
|
|
|
|
return arg.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline char const* sprintify(std::string const& arg)
|
|
|
|
{
|
|
|
|
return arg.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace gtr_detail
|
|
|
|
|
|
|
|
template<typename... Ts>
|
|
|
|
inline Glib::ustring gtr_sprintf(char const* fmt, Ts const&... args)
|
|
|
|
{
|
|
|
|
#if G_ENCODE_VERSION(GLIBMM_MAJOR_VERSION, GLIBMM_MINOR_VERSION) < G_ENCODE_VERSION(2, 62)
|
|
|
|
auto* const c_str = g_strdup_printf(fmt, gtr_detail::sprintify(args)...);
|
|
|
|
Glib::ustring ustr(c_str);
|
|
|
|
g_free(c_str);
|
|
|
|
|
|
|
|
return ustr;
|
|
|
|
#else
|
|
|
|
return Glib::ustring::sprintf(fmt, args...);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Ts>
|
|
|
|
inline Glib::ustring gtr_sprintf(Glib::ustring const& fmt, Ts const&... args)
|
|
|
|
{
|
|
|
|
#if G_ENCODE_VERSION(GLIBMM_MAJOR_VERSION, GLIBMM_MINOR_VERSION) < G_ENCODE_VERSION(2, 62)
|
|
|
|
return gtr_sprintf(fmt.c_str(), args...);
|
|
|
|
#else
|
|
|
|
return Glib::ustring::sprintf(fmt, args...);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T* gtr_get_ptr(Glib::RefPtr<T> const& ptr)
|
|
|
|
{
|
|
|
|
#if G_ENCODE_VERSION(GLIBMM_MAJOR_VERSION, GLIBMM_MINOR_VERSION) < G_ENCODE_VERSION(2, 56)
|
|
|
|
return ptr.operator->();
|
|
|
|
#else
|
|
|
|
return ptr.get();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
template<typename T, typename U>
|
|
|
|
inline Glib::RefPtr<T> gtr_ptr_static_cast(Glib::RefPtr<U> const& ptr)
|
|
|
|
{
|
|
|
|
#if G_ENCODE_VERSION(GLIBMM_MAJOR_VERSION, GLIBMM_MINOR_VERSION) < G_ENCODE_VERSION(2, 68)
|
|
|
|
return Glib::RefPtr<T>::cast_static(ptr);
|
|
|
|
#else
|
|
|
|
return std::static_pointer_cast<T>(ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
template<>
|
|
|
|
struct std::hash<Glib::ustring>
|
|
|
|
{
|
|
|
|
std::size_t operator()(Glib::ustring const& s) const
|
|
|
|
{
|
|
|
|
return std::hash<std::string>()(s.raw());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
template<>
|
|
|
|
struct fmt::formatter<Glib::ustring> : formatter<std::string>
|
|
|
|
{
|
|
|
|
template<typename FormatContext>
|
|
|
|
auto format(Glib::ustring const& ustr, FormatContext& ctx) const
|
|
|
|
{
|
|
|
|
return formatter<std::string>::format(ustr.raw(), ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace Glib
|
|
|
|
{
|
|
|
|
|
|
|
|
#if G_ENCODE_VERSION(GLIBMM_MAJOR_VERSION, GLIBMM_MINOR_VERSION) < G_ENCODE_VERSION(2, 68)
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline bool operator==(RefPtr<T> const& lhs, std::nullptr_t /*rhs*/)
|
|
|
|
{
|
|
|
|
return !lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline bool operator!=(RefPtr<T> const& lhs, std::nullptr_t /*rhs*/)
|
|
|
|
{
|
|
|
|
return !(lhs == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline RefPtr<T> make_refptr_for_instance(T* object)
|
|
|
|
{
|
|
|
|
return RefPtr<T>(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace Glib
|