Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use g_app_info_launch_default_for_uri for open url/path #578

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/iptux/ApplicationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ void do_action(Application* app, const string& name) {
g_action_activate(g_action_map_lookup_action(m, name.c_str()), NULL);
}


TEST(Application, Constructor) {
Application* app = CreateApplication();
do_action(app, "help.whats_new");
do_action(app, "tools.open_chat_log");
do_action(app, "tools.open_system_log");
DestroyApplication(app);
}
49 changes: 27 additions & 22 deletions src/iptux/UiHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,43 @@

static bool pop_disabled = false;

void iptux_open_path(const char* path) {
g_return_if_fail(!!path);

GError* error = nullptr;
gchar* uri = g_filename_to_uri(path, nullptr, &error);
if (error) {
LOG_WARN(_("Can't convert path to uri: %s, reason: %s"), path,

Check warning on line 26 in src/iptux/UiHelper.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/UiHelper.cpp#L26

Added line #L26 was not covered by tests
error->message);
g_error_free(error);
return;

Check warning on line 29 in src/iptux/UiHelper.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/UiHelper.cpp#L28-L29

Added lines #L28 - L29 were not covered by tests
}
g_app_info_launch_default_for_uri(uri, nullptr, &error);
if (error) {
LOG_WARN(_("Can't open path: %s, reason: %s"), path, error->message);
g_error_free(error);
}
g_free(uri);
}

/**
* 打开URL.
* @param url url
*/
void iptux_open_url(const char* url) {
int fd;
g_return_if_fail(!!url);

if (pop_disabled) {
LOG_INFO(_("Open URL: %s\n"), url);
if (url[0] == '/') {
iptux_open_path(url);
return;
}

if (fork() != 0)
return;

/* 关闭由iptux打开的所有可能的文件描述符 */
fd = 3;
while (fd < FD_SETSIZE) {
close(fd);
fd++;
GError* error = nullptr;
g_app_info_launch_default_for_uri(url, nullptr, &error);
if (error) {
LOG_WARN(_("Can't open URL: %s, reason: %s"), url, error->message);
g_error_free(error);
}
/* 脱离终端控制 */
setsid();

/* 打开URL */
execlp("xdg-open", "xdg-open", url, NULL);
/* 测试系统中所有可能被安装的浏览器 */
execlp("firefox", "firefox", url, NULL);
execlp("opera", "opera", url, NULL);
execlp("konqueror", "konqueror", url, NULL);
execlp("open", "open", url, NULL);
LOG_WARN(_("Can't find any available web browser!\n"));
}

bool ValidateDragData(GtkSelectionData* data,
Expand Down