~ubuntu-branches/ubuntu/jaunty/file-browser-applet/jaunty

« back to all changes in this revision

Viewing changes to src/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2008-06-01 18:59:43 UTC
  • Revision ID: james.westby@ubuntu.com-20080601185943-vwl5342pfbst8p3l
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * File:                                utils.c
 
3
 * Created:                             August 2007
 
4
 * Created by:                  Axel von Bertoldi
 
5
 * Last Modified:               January 2008
 
6
 * Last Modified by:    Axel von Bertoldi
 
7
 * (C) 2005-2008                Axel von Bertoldi
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify it
 
10
 * under the terms of the GNU General Public License as published by the Free
 
11
 * Software Foundation; either version 2 of the License, or (at your option)
 
12
 * any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
16
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 
17
 * more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License along with
 
20
 * this program; if not, write to:
 
21
 * The Free Software Foundation, Inc.,
 
22
 * 51 Franklin Street, Fifth Floor
 
23
 * Boston, MA 02110-1301, USA.
 
24
 */
 
25
 
 
26
#include <glib/gprintf.h>
 
27
#include "utils.h"
 
28
 
 
29
/******************************************************************************/
 
30
gboolean
 
31
utils_check_gerror (GError **error)
 
32
{
 
33
        if (*error)
 
34
        {               
 
35
                if (DEBUG) g_printf ("error: %s\n", (*error)->message);
 
36
                utils_show_dialog ("Application Error",
 
37
                                                   (*error)->message,
 
38
                                                   GTK_MESSAGE_ERROR);
 
39
                g_error_free (*error);
 
40
                *error = NULL;
 
41
                return TRUE;
 
42
        }
 
43
        return FALSE;
 
44
}
 
45
/******************************************************************************/
 
46
void
 
47
utils_show_dialog (gchar *title, gchar *message, GtkMessageType type)
 
48
{
 
49
        GtkWidget *dialog = gtk_message_dialog_new (NULL,
 
50
                                                                                                0,
 
51
                                                                                                type,
 
52
                                                                                                GTK_BUTTONS_CLOSE,
 
53
                                                                                                message);
 
54
        gtk_window_set_title (GTK_WINDOW (dialog), title);
 
55
 
 
56
        g_signal_connect (G_OBJECT (dialog), "destroy",
 
57
                                          G_CALLBACK (gtk_widget_destroy), dialog);
 
58
    g_signal_connect (G_OBJECT (dialog), "delete_event",
 
59
                      G_CALLBACK (gtk_widget_destroy), dialog);
 
60
        g_signal_connect_swapped (G_OBJECT (dialog),
 
61
                                                          "response", 
 
62
                                                          G_CALLBACK (gtk_widget_destroy),
 
63
                                                          dialog);
 
64
        gtk_widget_show_all (dialog);
 
65
 
 
66
        return;
 
67
}
 
68
/******************************************************************************/
 
69
GtkWidget*
 
70
utils_get_scaled_image_from_file (gchar *file_name, int size) {
 
71
                GdkPixbuf *orig   = gdk_pixbuf_new_from_file (file_name, NULL);
 
72
                if (orig == NULL) return NULL;
 
73
 
 
74
                GdkPixbuf *scaled = gdk_pixbuf_scale_simple (orig,
 
75
                                                                 size,
 
76
                                                                                                         size,
 
77
                                                                                                         GDK_INTERP_HYPER);
 
78
                GtkWidget *icon = gtk_image_new_from_pixbuf (scaled);
 
79
                g_object_unref (orig);
 
80
                g_object_unref (scaled);
 
81
                return icon;
 
82
}
 
83
/******************************************************************************/
 
84
GSList *
 
85
g_slist_swap_data (GSList *list, guint index) {
 
86
        gpointer tmp   = NULL;
 
87
        GSList *first  = g_slist_nth (list, index);
 
88
        GSList *second = first->next;
 
89
 
 
90
        tmp = first->data;
 
91
        first->data  = second->data;
 
92
        second->data = tmp;
 
93
        return list;
 
94
}
 
95
/******************************************************************************/
 
96
gint
 
97
utils_sort_alpha (const gchar **s1,
 
98
                                  const gchar **s2) {
 
99
        return g_utf8_collate ((gchar *)*s1, (gchar *)*s2);
 
100
}
 
101
/******************************************************************************/
 
102
gchar *
 
103
utils_clamp_file_name (const gchar *file_name, int length, gboolean *clamped) {
 
104
/* clamped is true if the string is actually clamped */
 
105
        gchar *tmp, *ret;
 
106
 
 
107
        if (strlen (file_name) > length) {
 
108
                tmp = g_strndup (file_name, length - 3);
 
109
                ret = g_strdup_printf ("%s...", tmp);
 
110
                g_free (tmp);
 
111
                if (clamped != NULL) *clamped = TRUE;
 
112
                return ret;
 
113
        }
 
114
        else {
 
115
                if (clamped != NULL) *clamped = FALSE;
 
116
                return g_strdup (file_name);
 
117
        }
 
118
}
 
119
/******************************************************************************/