📄 commonapp.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: commonapp.cpp,v 1.35.2.17 2004/11/24 23:00:23 rggammon Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** *//* Helix includes */#include "hxcom.h"#include "hxwintyp.h"#include "hxstring.h"#include "hxurl.h"#include "commonapp.h"#include "mainapp.h"#include "auth.h"#include "error.h"#include "upgrade.h"#include "remote.h"#include "switchboard.h"#include "appver.h"#include "hlxclib/string.h"#include "hlxclib/stdio.h"#include "hlxclib/stdlib.h"#include "hlxclib/stdarg.h"#include "hlxclib/getopt.h"#include "hlxclib/ctype.h"#ifdef G_OS_UNIX#include <libgen.h>#include <sys/types.h>#include <sys/stat.h>#include "embeddedapp.h"#endif#include <glib.h>#include "winutils.h"#include "hxplayer.h"#include "hxstatisticsobserver.h"#include "hxgvalue.h"#include "hxgprefs.h"#include "hxgerror.h"#include "favorites.h"#include <unistd.h>#include <locale.h>#include "hxplayer-i18n.h"#define EXIT_RESTART_PLAYER 10#define MAX_TITLE_LENGTH 60HXCommonApp g_hxcommon_app;/* hxcommon_* functions * ==================== * Utility functions that perform operations common to both the embedded * and top-level player. *//* WARNING! This function will modify the string it is passed */gchar*hxcommon_strtrim(gchar* str){ gchar* start = str; gchar* end; gchar* mark = NULL; /* Find the first non-space character */ while(*start && isspace(*start)) { start++; } end = start; /* trim off any trailing whitespace */ while(*end) { if(*end && !isspace(*end)) { mark = end; } end++; } if(mark) { /* Null terminate after the last non-space character */ mark++; *mark = '\0'; } return start;}gchar*hxcommon_strdup_trim(const gchar* str){ const gchar* start = str; gchar* end; gchar* str_out; gchar* mark = NULL; /* Find the first non-space character */ while(*start && isspace(*start)) { start++; } str_out = g_strdup(start); end = str_out; /* trim off any trailing whitespace */ while(*end) { if(*end && !isspace(*end)) { mark = end; } end++; } if(mark) { /* Null terminate after the last non-space character */ mark++; *mark = '\0'; } return str_out;}gchar*hxcommon_escape_underscores(const char* str){ gint buf_size = 0; gchar* buf; const gchar* src_pos; gchar* dest_pos; /* Pass 1: count the underscores */ src_pos = str; while(*src_pos) { if(*src_pos == '_') { buf_size += 2; } else { buf_size++; } src_pos++; } buf_size++; /* Pass 2: Allocate and copy */ buf = (gchar*)g_try_malloc(buf_size); g_return_val_if_fail(buf != NULL, NULL); src_pos = str; dest_pos = buf; while(*src_pos) { if(*src_pos == '_') { /* Add an extra underscore */ *dest_pos++ = '_'; } *dest_pos++ = *src_pos++; } *dest_pos = '\0'; return buf;}gchar*hxcommon_get_title_from_url(const gchar* url){ const gchar* pos; gchar* p; gint len = 0; gchar* title; const gchar* slash_pos = NULL; guint i; pos = url; while(*pos) { if(*pos == '/') { slash_pos = pos; } else if(*pos == '?') { break; } pos++; } if(slash_pos) { len = pos - slash_pos; } else { len = pos - url - 1; } if(!slash_pos || len == 0) { title = g_strdup(_("untitled")); } else { /* Decode the title */ gchar* encoded_filename; gchar* filename; gchar c; encoded_filename = (gchar*) g_malloc(len + 1); strncpy(encoded_filename, slash_pos + 1, len); encoded_filename[len] = '\0'; filename = hxcommon_strdup_and_unescape_url(encoded_filename); g_free(encoded_filename); /* Filename is in utf-8 at this point. Truncate if necessary. */ c = g_utf8_get_char (filename); p = filename; i = 0; while(c) { p = g_utf8_next_char(p); c = g_utf8_get_char (p); i++; if(i == MAX_TITLE_LENGTH) { /* Back up 3 characters */ p = g_utf8_prev_char(p); p = g_utf8_prev_char(p); p = g_utf8_prev_char(p); /* XXXRGG: Not sure if this is valid for all languages... */ *p++ = '.'; *p++ = '.'; *p++ = '.'; *p++ = '\0'; /* Trim the buffer */ filename = (gchar*) g_realloc(filename, p - filename); break; } } title = filename; } return title;}/* Caller must free the returned string */gchar*hxcommon_locate_file(const gchar *file_name){#ifdef USE_GNOME /* prepend the package prefix */ gchar* result; gchar* package_file_name = g_strdup_printf("%s/%s", g_hxcommon_app.package, file_name); result = gnome_program_locate_file(NULL, GNOME_FILE_DOMAIN_APP_DATADIR, package_file_name, FALSE, NULL); g_free(package_file_name); return result;#else guint i; gchar* resolved_path = NULL; gchar* resource_paths[] = { "", "/" APP_NAME_SHORT, "/default" };#ifndef HELIX_FEATURE_LIBGLADE /* Ignore requests for .glade files -- we don't need them */ if(strstr(file_name, ".glade")) { return g_strdup(file_name); }#endif for(i = 0; i < sizeof(resource_paths) / sizeof(*resource_paths); i++) { resolved_path = g_strdup_printf("%s/%s%s/%s", g_hxcommon_app.package_data_dir, g_hxcommon_app.package, resource_paths[i], file_name); if(g_file_test(resolved_path, G_FILE_TEST_EXISTS)) { break; } g_free(resolved_path); resolved_path = NULL; } return resolved_path;#endif}gbooleanhxcommon_url_show(const gchar* orig_url){ gboolean ret = FALSE; gchar* argv[3]; gchar* url; url = g_strdup(orig_url); argv[1] = url; argv[2] = NULL; if(g_hxcommon_app.web_browser_command) { argv[0] = g_hxcommon_app.web_browser_command; /* Use the user-specified browser */ ret = g_spawn_async (NULL, /* working directory */ argv, NULL, /* envp */ G_SPAWN_SEARCH_PATH, /* flags */ NULL, /* child_setup */ NULL, /* data */ NULL, /* child_pid */ NULL); } if(!ret) { #ifdef USE_GNOME GError *error = NULL; gnome_url_show(url, &error); if (error != NULL) { g_warning (error->message); g_error_free (error); }#else /* try some of the usual suspects */ guint i; gchar* browser_commands[] = { "htmlview", /* RedHat */ "x-www-browser", /* Debian */ "firefox", "MozillaFirebird", "mozilla", "opera", "epiphany", "konqueror", "nautilus", "galeon", "iexplore" /* Windows */ }; for(i = 0; i < sizeof(browser_commands) / sizeof(*browser_commands); i++) { argv[0] = browser_commands[i]; ret = g_spawn_async (NULL, /* working directory */ argv, NULL, /* envp */ G_SPAWN_SEARCH_PATH, /* flags */ NULL, /* child_setup */ NULL, /* data */ NULL, /* child_pid */ NULL); if(ret) { g_hxcommon_app.web_browser_command = g_strdup(browser_commands[i]); break; } } } #endif /* USE_GNOME */ if(!ret) { GtkWidget* dialog; dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Failed to launch a web browser for help")); gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); } g_free(url); return ret; }voidhxcommon_close_dialog(GtkWidget* dialog, gint response, gpointer* data_ptr){ if(response != GTK_RESPONSE_HELP && response != GTK_RESPONSE_APPLY) { gtk_widget_destroy(dialog); *data_ptr = NULL; }}/* We do transient setting this way so we can pop up this dialog from the embedded player, where we don't have a GtkWindow to work with. */voidhxcommon_embedded_transient_parent_realized (GtkWidget *dialog, GdkWindow *parent){ if (GTK_WIDGET_REALIZED (dialog)) { gdk_window_set_transient_for (dialog->window, parent); }}voidhxcommon_launch_error_help_with_widget_name(GtkWidget* widget){ gchar* url; GtkWidget* toplevel; guint error_code; toplevel = hxcommon_get_toplevel_widget_no_menus(widget); error_code = (guint)g_object_get_data(G_OBJECT(toplevel), "error_code"); url = hx_switchboard_get_error_help_url(error_code); hxcommon_url_show(url); g_free(url); }voidhxcommon_launch_upgrade_with_widget_name(GtkWidget* widget){ gchar* url; GtkWidget* toplevel; gchar* components; toplevel = hxcommon_get_toplevel_widget_no_menus(widget); components = (gchar*)g_object_get_data(G_OBJECT(toplevel), "components"); url = hx_switchboard_get_upgrade_url (components); hxcommon_url_show(url); g_free(url); }voidhxcommon_launch_context_help(const gchar* context){ gchar* url; url = hx_switchboard_get_context_help_url(context); hxcommon_url_show(url); g_free(url);}voidhxcommon_launch_context_help_with_widget_name(GtkWidget* widget){ /* Glade's handling of user data leaves much to be desired, so we use the widget name as the context we pass to the wiki. */ const gchar* context; context = gtk_widget_get_name(widget);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -