~ubuntu-branches/ubuntu/trusty/gnome-paint/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/debian-612470-handle-urls.patch/src/file.c

  • Committer: Bazaar Package Importer
  • Author(s): Aron Xu
  • Date: 2011-02-10 14:34:01 UTC
  • Revision ID: james.westby@ubuntu.com-20110210143401-1nsou6k5ky2zvohy
Tags: 0.4.0-2
* debian/patches/debian-612470-handle-urls.patch:
  + Handle URLs when given as command line parameters. (Closes: #612470)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *            file.c
 
3
 *
 
4
 *  Thu Jun 11 13:19:47 2009
 
5
 *  Copyright  2009  Rogério Ferro
 
6
 *  <rogerioferro@gmail.com>
 
7
 ****************************************************************************/
 
8
 
 
9
/*
 
10
 * This program is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 * 
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Library General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 
23
 */
 
24
 
 
25
#include "common.h"
 
26
#include "file.h"
 
27
#include "pixbuf-file-chooser.h"
 
28
#include "cv_drawing.h"
 
29
#include "undo.h"
 
30
 
 
31
#include <glib/gi18n.h>
 
32
#include <gtk/gtk.h>
 
33
#include <gdk-pixbuf/gdk-pixbuf.h>
 
34
 
 
35
/*private functions*/
 
36
static void             file_print_title        (void);
 
37
static void             file_set_name           (const char *name);
 
38
static void             file_set_type           (const char *type);
 
39
static void             file_set_title          (const char *title);
 
40
static void             file_set_untitle        (void);
 
41
static gboolean file_save                       (const gchar *filename, const gchar *type);
 
42
static void             file_error_dlg          (const gchar *message, const GError *error);
 
43
 
 
44
/*private data*/
 
45
GtkWindow       *parent_window  =       NULL;
 
46
gchar           *file_name              =       NULL;
 
47
gchar           *file_title             =       NULL;
 
48
gchar           *file_type              =       NULL;
 
49
gboolean        b_saved                 =       TRUE;
 
50
gboolean        b_untitle               =       TRUE;
 
51
 
 
52
 
 
53
/*
 
54
 *   CODE
 
55
 */
 
56
 
 
57
void 
 
58
file_set_parent_window  ( GtkWindow * wnd )
 
59
{
 
60
        parent_window   =       wnd;
 
61
        b_saved =       TRUE;
 
62
        file_set_untitle ();
 
63
}
 
64
 
 
65
gboolean
 
66
file_is_save ( void )
 
67
{
 
68
    return b_saved;
 
69
}
 
70
 
 
71
void            
 
72
file_set_unsave ( void )
 
73
{
 
74
        b_saved =       FALSE;
 
75
        file_print_title ();
 
76
}
 
77
 
 
78
void
 
79
file_set_save ( void )
 
80
{
 
81
        b_saved =       TRUE;
 
82
        file_print_title ();    
 
83
}
 
84
 
 
85
gboolean
 
86
file_save_dialog ( void )
 
87
{
 
88
        gboolean cancel = FALSE;
 
89
        if (!b_saved)
 
90
        {
 
91
                gint result;
 
92
                GtkWidget *dlg;
 
93
        dlg = gtk_message_dialog_new(
 
94
               parent_window, 
 
95
               GTK_DIALOG_MODAL, 
 
96
               GTK_MESSAGE_QUESTION, 
 
97
               GTK_BUTTONS_NONE, 
 
98
               _("Do you want to save the changes you made to \"%s\"?"),
 
99
               file_title);
 
100
                gtk_message_dialog_format_secondary_text(
 
101
                    GTK_MESSAGE_DIALOG (dlg), 
 
102
                    _("Your changes will be lost if you don't save them.") );
 
103
 
 
104
                gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_DISCARD, GTK_RESPONSE_NO);
 
105
        gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
 
106
        gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
 
107
                gtk_dialog_set_default_response (GTK_DIALOG(dlg), GTK_RESPONSE_YES);
 
108
 
 
109
                result = gtk_dialog_run (GTK_DIALOG (dlg));
 
110
                gtk_widget_destroy (dlg);
 
111
                switch (result)
 
112
        {
 
113
            case GTK_RESPONSE_YES:
 
114
                                on_menu_save_activate (NULL, NULL);
 
115
                if ( b_saved )
 
116
                {
 
117
                    cancel = FALSE;
 
118
                }
 
119
                else
 
120
                {
 
121
                    cancel = TRUE;
 
122
                }
 
123
                break;
 
124
            case GTK_RESPONSE_CANCEL:
 
125
                cancel = TRUE;
 
126
                break;
 
127
         }
 
128
        }
 
129
        return cancel;
 
130
}
 
131
 
 
132
 
 
133
 
 
134
 
 
135
gboolean        
 
136
file_open ( const gchar * filename )
 
137
{
 
138
        gboolean        ok              =       FALSE;
 
139
        GError          *error  = NULL;
 
140
        GdkPixbuf       *pixbuf = NULL;
 
141
        pixbuf = gdk_pixbuf_new_from_file (filename, &error);
 
142
        if (pixbuf != NULL)
 
143
        {
 
144
                if (!file_save_dialog () )
 
145
                {
 
146
                        GdkPixbufFormat *format =       gdk_pixbuf_get_file_info (filename, NULL, NULL);
 
147
                        GdkPixbuf               *orientation_changed_pixbuf;
 
148
                        orientation_changed_pixbuf      =       gdk_pixbuf_apply_embedded_orientation (pixbuf);
 
149
                        cv_set_pixbuf   ( orientation_changed_pixbuf );
 
150
                        g_object_unref  ( orientation_changed_pixbuf );
 
151
            undo_clear ();
 
152
                        ok      =       TRUE;
 
153
                        if (gdk_pixbuf_format_is_writable (format))
 
154
                        {
 
155
                                gchar   *title  =       g_filename_display_basename (filename);
 
156
                                gchar   *type   =       gdk_pixbuf_format_get_name (format);
 
157
                                b_untitle       =       FALSE;
 
158
                                b_saved         =       TRUE;
 
159
                                file_set_type   (type);
 
160
                                file_set_name   (filename);
 
161
                                file_set_title  (title);
 
162
                                g_free (title);
 
163
                                g_free (type);
 
164
                        }
 
165
                        else
 
166
                        {
 
167
                                b_saved =       FALSE;
 
168
                                file_set_untitle ();
 
169
                        }
 
170
                }
 
171
        }
 
172
        else
 
173
        {
 
174
                gchar *basename =       g_path_get_basename (filename);
 
175
                gchar *message  =       g_strdup_printf (_("Error loading file \"%s\""), basename);
 
176
                file_error_dlg ( message, error);
 
177
                g_error_free (error);
 
178
                g_free (basename);
 
179
                g_free (message);
 
180
        }
 
181
        g_object_unref (pixbuf);
 
182
        return ok;
 
183
}
 
184
 
 
185
 
 
186
 
 
187
/* GUI CallBacks */
 
188
 
 
189
void
 
190
on_menu_open_activate   ( GtkMenuItem *menuitem, gpointer user_data)
 
191
{
 
192
        GtkWidget *dialog;
 
193
        gint response;
 
194
 
 
195
        dialog = pixbuf_file_chooser_new (parent_window, GTK_FILE_CHOOSER_ACTION_OPEN);
 
196
 
 
197
        response = gtk_dialog_run (GTK_DIALOG (dialog));
 
198
        gtk_widget_hide (dialog);
 
199
 
 
200
        if (response == GTK_RESPONSE_OK) 
 
201
        {
 
202
                gchar * name    = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
 
203
                file_open (name);
 
204
                g_free (name);
 
205
        }
 
206
        gtk_widget_destroy (dialog);
 
207
}
 
208
 
 
209
void 
 
210
on_menu_save_activate   ( GtkMenuItem *menuitem, gpointer user_data)
 
211
{
 
212
        if (b_untitle)
 
213
        {
 
214
                on_menu_save_as_activate (menuitem, user_data);
 
215
        }
 
216
        else
 
217
        {
 
218
                if (file_save (file_name, file_type))
 
219
                {
 
220
                        b_saved         =       TRUE;
 
221
                        file_print_title ();
 
222
                }
 
223
        }
 
224
}
 
225
 
 
226
void
 
227
on_menu_save_as_activate        ( GtkMenuItem *menuitem, gpointer user_data)
 
228
{
 
229
        GtkWidget *dialog;
 
230
        gint response;
 
231
 
 
232
        dialog = pixbuf_file_chooser_new (parent_window, GTK_FILE_CHOOSER_ACTION_SAVE);
 
233
        pixbuf_file_chooser_set_current_name ( PIXBUF_FILE_CHOOSER (dialog), file_title);
 
234
        pixbuf_file_chooser_set_current_filter( PIXBUF_FILE_CHOOSER (dialog), "png");
 
235
 
 
236
        response = gtk_dialog_run (GTK_DIALOG (dialog));
 
237
        gtk_widget_hide (dialog);
 
238
        
 
239
        if (response == GTK_RESPONSE_OK) 
 
240
        {
 
241
                gchar * name                            = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
 
242
                gchar * title                           = pixbuf_file_chooser_get_name (PIXBUF_FILE_CHOOSER(dialog));
 
243
                GdkPixbufFormat * format        = pixbuf_file_chooser_get_format (PIXBUF_FILE_CHOOSER(dialog));
 
244
                gchar * type                            = gdk_pixbuf_format_get_name (format);
 
245
 
 
246
 
 
247
                if (file_save (name, type))
 
248
                {
 
249
                        b_untitle       =       FALSE;
 
250
                        b_saved         =       TRUE;
 
251
 
 
252
                        file_set_name   (name);
 
253
                        file_set_type   (type);
 
254
                        file_set_title  (title);
 
255
                }
 
256
                
 
257
                g_free (name);
 
258
                g_free (title);
 
259
                g_free (type);
 
260
        }
 
261
        gtk_widget_destroy (dialog);    
 
262
}
 
263
 
 
264
 
 
265
/*Private*/
 
266
 
 
267
static void
 
268
file_print_title (void)
 
269
{
 
270
        gchar *str;
 
271
 
 
272
        str = g_strdup_printf (_("%s - gnome-paint"), file_title);
 
273
        
 
274
        if (!b_saved)
 
275
        {
 
276
                gchar *copy;
 
277
                copy = g_strdup_printf ("%c%s", '*', str);
 
278
                g_free (str);
 
279
                str = copy;
 
280
        }
 
281
 
 
282
        gtk_window_set_title ( parent_window, str);
 
283
 
 
284
        g_free (str);
 
285
}
 
286
 
 
287
static void
 
288
file_set_name (const char *name)
 
289
{
 
290
        g_free (file_name);
 
291
        file_name = g_strdup (name);
 
292
}
 
293
 
 
294
static void
 
295
file_set_type (const char *type)
 
296
{
 
297
        g_free (file_type);
 
298
        file_type = g_strdup (type);
 
299
}
 
300
 
 
301
static void 
 
302
file_set_title  (const char *title)
 
303
{
 
304
        g_free( file_title );
 
305
        file_title      = g_strdup (title);     
 
306
        file_print_title ();
 
307
}
 
308
 
 
309
static void
 
310
file_set_untitle (void)
 
311
{
 
312
        b_untitle               =       TRUE;
 
313
        file_set_title (_("untitled"));
 
314
}
 
315
 
 
316
static gboolean
 
317
file_save (const gchar *filename, const gchar *type)
 
318
{
 
319
        gboolean        ok              =       TRUE;
 
320
        GdkPixbuf *     pixbuf  =       cv_get_pixbuf ();
 
321
        GError *        error   =       NULL;
 
322
 
 
323
        if ( !gdk_pixbuf_save ( pixbuf, filename, type, &error, NULL) )
 
324
        {
 
325
                gchar *basename =       g_path_get_basename (filename);
 
326
                gchar *message  =       g_strdup_printf (_("Error saving file \"%s\""), basename);
 
327
                file_error_dlg ( message, error);
 
328
                g_error_free (error);
 
329
                g_free (basename);
 
330
                g_free (message);
 
331
                ok                              =       FALSE;
 
332
        }
 
333
        g_object_unref (pixbuf);
 
334
        return ok;
 
335
}
 
336
 
 
337
static void
 
338
file_error_dlg (const gchar *message, const GError *error)
 
339
{
 
340
                gchar *error_message = _("unknown error");
 
341
                GtkWidget *dlg;
 
342
                if (error !=  NULL )
 
343
                {
 
344
                        error_message = error->message;
 
345
                }
 
346
                g_warning ("%s: %s\n", message, error_message);
 
347
                dlg = gtk_message_dialog_new ( parent_window,
 
348
                                               GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
 
349
                                           GTK_MESSAGE_ERROR,
 
350
                                           GTK_BUTTONS_CLOSE,
 
351
                                       "%s",message);
 
352
                gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG (dlg), "%s",error_message);
 
353
                gtk_dialog_run (GTK_DIALOG (dlg));
 
354
                gtk_widget_destroy (dlg);
 
355
}