1 /*
2  * Copyright © 2009-2010 freedcpp, http://code.google.com/p/freedcpp
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * In addition, as a special exception, compiling, linking, and/or
19  * using OpenSSL with this program is allowed.
20  */
21 
22 #include "settingsmanager.hh"
23 #include "WulforUtil.hh"
24 #include <glib/gi18n.h>
25 #include "previewmenu.hh"
26 
27 using namespace std;
28 using namespace dcpp;
29 
cleanMenu_gui()30 void PreviewMenu::cleanMenu_gui()
31 {
32     gtk_container_foreach(GTK_CONTAINER(appsPreviewMenu), (GtkCallback)gtk_widget_destroy, NULL);
33 }
34 
buildMenu_gui(const string & target)35 bool PreviewMenu::buildMenu_gui(const string &target)
36 {
37     if (target == "none" || target.empty())
38         return FALSE;
39 
40     string file = Util::getFileName(target);
41     string ext = Util::getFileExt(file);
42 
43     if (ext == ".dctmp")
44     {
45         if (count(file.begin(), file.end(), '.') >= 3)
46         {
47             string::size_type i = file.rfind('.');
48             string::size_type j = file.rfind('.', i - 1);
49             ext = Util::getFileExt(file.substr(0, j));
50         }
51         else
52             ext = "";
53     }
54 
55     if (ext.empty() || ext == "." || file == ext)
56         return FALSE;
57 
58     ext.erase(0, 1);
59 
60     GtkWidget* itemApp;
61     string appExtensions = "";
62     ext = Text::toLower(ext);
63 
64     const PreviewApp::List &Apps = WulforSettingsManager::getInstance()->getPreviewApps();
65 
66     for (PreviewApp::Iter item = Apps.begin(); item != Apps.end(); ++item)
67     {
68         appExtensions = Text::toLower((*item)->ext);
69 
70         if (appExtensions.find(ext) != string::npos)
71         {
72             itemApp = gtk_menu_item_new_with_label(((*item)->name).c_str());
73 
74             gtk_menu_shell_append(GTK_MENU_SHELL(appsPreviewMenu), itemApp);
75 
76             g_signal_connect(itemApp, "activate", G_CALLBACK(onPreviewAppClicked_gui), (gpointer) this);
77 
78             g_object_set_data_full(G_OBJECT(itemApp), "command", g_strdup("application"), g_free);
79             g_object_set_data_full(G_OBJECT(itemApp), "application", g_strdup(((*item)->app).c_str()), g_free);
80             g_object_set_data_full(G_OBJECT(itemApp), "target", g_strdup(target.c_str()), g_free);
81         }
82     }
83 
84     itemApp = gtk_separator_menu_item_new();
85     gtk_menu_shell_append(GTK_MENU_SHELL(appsPreviewMenu), itemApp);
86 
87     itemApp = gtk_menu_item_new_with_label(_("Default"));
88     gtk_menu_shell_append(GTK_MENU_SHELL(appsPreviewMenu), itemApp);
89 
90     g_signal_connect(itemApp, "activate", G_CALLBACK(onPreviewAppClicked_gui), (gpointer) this);
91 
92     g_object_set_data_full(G_OBJECT(itemApp), "command", g_strdup("default"), g_free);
93     g_object_set_data_full(G_OBJECT(itemApp), "application", g_strdup(""), g_free);
94     g_object_set_data_full(G_OBJECT(itemApp), "target", g_strdup(target.c_str()), g_free);
95 
96     return TRUE;
97 }
98 
onPreviewAppClicked_gui(GtkMenuItem * menuItem,gpointer data)99 void PreviewMenu::onPreviewAppClicked_gui(GtkMenuItem *menuItem, gpointer data)
100 {
101     string command = (gchar *) g_object_get_data(G_OBJECT(menuItem), "command");
102     string application = (gchar *) g_object_get_data(G_OBJECT(menuItem), "application");
103     string target = (gchar *) g_object_get_data(G_OBJECT(menuItem), "target");
104 
105     if(command == "default") WulforUtil::openURI(target);
106     else
107     {
108         string cmd = application + " \"" + target + "\"";
109         WulforUtil::openURItoApp(cmd);
110     }
111 }
112