Move some functions from main.[ch] to new window.[ch].
[geeqie.git] / src / window.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 The Geeqie Team
4  *
5  * Authors: Vladimir Nadvornik / Laurent Monin
6  * 
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include "main.h"
13 #include "window.h"
14
15 #include "debug.h"
16 #include "pixbuf_util.h"
17 #include "ui_fileops.h"
18 #include "ui_help.h"
19
20 GtkWidget *window_new(GtkWindowType type, const gchar *name, const gchar *icon,
21                       const gchar *icon_file, const gchar *subtitle)
22 {
23         gchar *title;
24         GtkWidget *window;
25
26         window = gtk_window_new(type);
27         if (!window) return NULL;
28
29         if (subtitle)
30                 {
31                 title = g_strdup_printf("%s - %s", subtitle, GQ_APPNAME);
32                 }
33         else
34                 {
35                 title = g_strdup_printf("%s", GQ_APPNAME);
36                 }
37
38         gtk_window_set_title(GTK_WINDOW(window), title);
39         g_free(title);
40
41         window_set_icon(window, icon, icon_file);
42         gtk_window_set_role(GTK_WINDOW(window), name);
43         gtk_window_set_wmclass(GTK_WINDOW(window), name, GQ_WMCLASS);
44
45         return window;
46 }
47
48 void window_set_icon(GtkWidget *window, const gchar *icon, const gchar *file)
49 {
50         if (!icon && !file) icon = PIXBUF_INLINE_ICON;
51
52         if (icon)
53                 {
54                 GdkPixbuf *pixbuf;
55
56                 pixbuf = pixbuf_inline(icon);
57                 if (pixbuf)
58                         {
59                         gtk_window_set_icon(GTK_WINDOW(window), pixbuf);
60                         g_object_unref(pixbuf);
61                         }
62                 }
63         else
64                 {
65                 gtk_window_set_icon_from_file(GTK_WINDOW(window), file, NULL);
66                 }
67 }
68
69 gint window_maximized(GtkWidget *window)
70 {
71         GdkWindowState state;
72
73         if (!window || !window->window) return FALSE;
74
75         state = gdk_window_get_state(window->window);
76         return (state & GDK_WINDOW_STATE_MAXIMIZED);
77 }
78
79 /*
80  *-----------------------------------------------------------------------------
81  * Open  browser with the help Documentation
82  *-----------------------------------------------------------------------------
83  */
84
85 static gchar *command_result(const gchar *binary, const gchar *command)
86 {
87         gchar *result = NULL;
88         FILE *f;
89         char buf[2048];
90         int l;
91
92         if (!binary) return NULL;
93         if (!file_in_path(binary)) return NULL;
94
95         if (!command) return g_strdup(binary);
96         if (command[0] == '!') return g_strdup(command + 1);
97
98         f = popen(command, "r");
99         if (!f) return NULL;
100
101         while ((l = fread(buf, sizeof(char), sizeof(buf), f)) > 0)
102                 {
103                 if (!result)
104                         {
105                         int n = 0;
106
107                         while (n < l && buf[n] != '\n' && buf[n] != '\r') n++;
108                         if (n > 0) result = g_strndup(buf, n);
109                         }
110                 }
111
112         pclose(f);
113
114         return result;
115 }
116
117 static void help_browser_command(const gchar *command, const gchar *path)
118 {
119         gchar *result;
120         gchar *buf;
121         gchar *begin;
122         gchar *end;
123
124         if (!command || !path) return;
125
126         DEBUG_1("Help command pre \"%s\", \"%s\"", command, path);
127
128         buf = g_strdup(command);
129         begin = strstr(buf, "%s");
130         if (begin)
131                 {
132                 *begin = '\0';
133                 end = begin + 2;
134                 begin = buf;
135
136                 result = g_strdup_printf("%s%s%s &", begin, path, end);
137                 }
138         else
139                 {
140                 result = g_strdup_printf("%s \"%s\" &", command, path);
141                 }
142         g_free(buf);
143
144         DEBUG_1("Help command post [%s]", result);
145
146         system(result);
147
148         g_free(result);
149 }
150
151 /*
152  * each set of 2 strings is one browser:
153  *   the 1st is the binary to look for in the path
154  *   the 2nd has 3 capabilities:
155  *        NULL     exec binary with html file path as command line
156  *        string   exec string and use results for command line
157  *        !string  use text following ! as command line, replacing optional %s with html file path
158 */
159 static gchar *html_browsers[] =
160 {
161         /* Redhat has a nifty htmlview script to start the user's preferred browser */
162         "htmlview",     NULL,
163         /* GNOME 2 */
164         "gconftool-2",  "gconftool-2 -g /desktop/gnome/url-handlers/http/command",
165         /* KDE */
166         "kfmclient",    "!kfmclient exec \"%s\"",
167         /* use fallbacks */
168         "firefox",      NULL,
169         "mozilla",      NULL,
170         "konqueror",    NULL,
171         "netscape",     NULL,
172         NULL,           NULL
173 };
174
175 static void help_browser_run(void)
176 {
177         gchar *result = NULL;
178         gint i;
179
180         i = 0;
181         while (!result && html_browsers[i])
182                 {
183                 result = command_result(html_browsers[i], html_browsers[i+1]);
184                 i += 2;
185                 }
186
187         if (!result)
188                 {
189                 printf("Unable to detect an installed browser.\n");
190                 return;
191                 }
192
193         help_browser_command(result, GQ_HTMLDIR "/index.html");
194
195         g_free(result);
196 }
197
198 /*
199  *-----------------------------------------------------------------------------
200  * help window
201  *-----------------------------------------------------------------------------
202  */
203
204 static GtkWidget *help_window = NULL;
205
206 static void help_window_destroy_cb(GtkWidget *window, gpointer data)
207 {
208         help_window = NULL;
209 }
210
211 void help_window_show(const gchar *key)
212 {
213         if (key && strcmp(key, "html_contents") == 0)
214                 {
215                 help_browser_run();
216                 return;
217                 }
218
219         if (help_window)
220                 {
221                 gtk_window_present(GTK_WINDOW(help_window));
222                 if (key) help_window_set_key(help_window, key);
223                 return;
224                 }
225
226         help_window = help_window_new(_("Help"), GQ_WMCLASS, "help",
227                                       GQ_HELPDIR "/README", key);
228
229         g_signal_connect(G_OBJECT(help_window), "destroy",
230                          G_CALLBACK(help_window_destroy_cb), NULL);
231 }
232