~ubuntu-branches/ubuntu/saucy/synaptic/saucy

« back to all changes in this revision

Viewing changes to gtk/rgutils.cc

  • Committer: Package Import Robot
  • Author(s): Michael Vogt
  • Date: 2013-05-08 21:11:11 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130508211111-p6sq5df1u3r1de10
Tags: 0.80
update translations from launchpad

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include <apt-pkg/fileutl.h>
24
24
 
25
 
#include <X11/Xlib.h>
26
25
#include <gdk/gdkx.h>
27
26
#include <gtk/gtk.h>
28
27
#include <string>
30
29
#include <cstdlib>
31
30
#include <cstring>
32
31
 
 
32
#include <iostream>
 
33
 
33
34
#include "i18n.h"
34
35
#include "rgutils.h"
35
36
 
36
37
 
 
38
// helper
 
39
GdkPixbuf *
 
40
get_gdk_pixbuf(const gchar *name, int size)
 
41
{
 
42
   GtkIconTheme *theme;
 
43
   GdkPixbuf *pixbuf;
 
44
   GError *error = NULL;
 
45
 
 
46
   theme = gtk_icon_theme_get_default();
 
47
   pixbuf = gtk_icon_theme_load_icon(theme, name, size, 
 
48
                                     (GtkIconLookupFlags)0, &error);
 
49
   if (pixbuf == NULL) 
 
50
      std::cerr << "Warning, failed to load: " << name 
 
51
                << error->message << std::endl;
 
52
 
 
53
   return pixbuf;
 
54
}
 
55
 
 
56
GtkWidget *get_gtk_image(const gchar *name, int size)
 
57
{
 
58
   GdkPixbuf *buf;
 
59
   buf = get_gdk_pixbuf(name, size);
 
60
   if(!buf)
 
61
      return NULL;
 
62
   return gtk_image_new_from_pixbuf(buf);
 
63
}
 
64
 
37
65
void RGFlushInterface()
38
66
{
39
67
   XSync(GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), False);
84
112
   return S;
85
113
}
86
114
 
 
115
std::vector<const gchar*> GetBrowserCommand(const gchar *link)
 
116
{
 
117
   std::vector<const gchar*> cmd;
 
118
   if (FileExists("/usr/bin/xdg-open")) {
 
119
      cmd.push_back("/usr/bin/xdg-open");
 
120
      cmd.push_back(link);
 
121
   } else if (FileExists("/usr/bin/firefox")) {
 
122
      cmd.push_back("/usr/bin/firefox");
 
123
      cmd.push_back(link);
 
124
   } else if (FileExists("/usr/bin/konqueror")) {
 
125
      cmd.push_back("/usr/bin/konqueror");
 
126
      cmd.push_back(link);
 
127
   }
 
128
   return cmd;
 
129
}
 
130
 
 
131
bool RunAsSudoUserCommand(std::vector<const gchar*> cmd)
 
132
{
 
133
   std::vector<const gchar*> prefix;
 
134
    gchar *sudo_user;
 
135
 
 
136
    if (cmd.empty()) {
 
137
       std::cerr << "Empty command for RunAsSudoUserCommand" << std::endl;
 
138
       return true;
 
139
    }
 
140
 
 
141
    // try pkexec first, then sudo
 
142
    sudo_user = getenv("PKEXEC_UID");
 
143
    if (sudo_user == NULL) {
 
144
       sudo_user = getenv("SUDO_USER");
 
145
    }
 
146
#if 0 // does not work for some reason
 
147
    if(FileExists("/usr/bin/pkexec") && sudo_user != NULL)
 
148
    {
 
149
       prefix.push_back("/usr/bin/pkexec");
 
150
       prefix.push_back("--user");
 
151
       prefix.push_back(sudo_user);
 
152
    }
 
153
#endif
 
154
    if(FileExists("/usr/bin/sudo") && sudo_user != NULL)
 
155
    {
 
156
       prefix.push_back("/usr/bin/sudo");
 
157
       prefix.push_back("-u");
 
158
       prefix.push_back(sudo_user);
 
159
    }
 
160
    // insert the prefix string
 
161
    cmd.insert(cmd.begin(), prefix.begin(), prefix.end());
 
162
 
 
163
#if 0
 
164
    for(std::vector<const gchar*>::iterator it = cmd.begin();
 
165
        it != cmd.end(); it++)
 
166
       printf("cmd '%s'\n", *it);
 
167
#endif
 
168
 
 
169
    // build the c way to make g_spawn_async happy
 
170
    char **c_cmd = new char*[cmd.size()+1];
 
171
    int i;
 
172
    for(i=0; i<cmd.size(); i++)
 
173
       c_cmd[i] = (gchar*)cmd[i];
 
174
    c_cmd[i] = NULL;
 
175
 
 
176
    GError *error = NULL;
 
177
    g_spawn_async("/", c_cmd, NULL, (GSpawnFlags)0, NULL, NULL, NULL, &error);
 
178
    if (error != NULL) {
 
179
       std::cerr << "Failed to run cmd: " << cmd[0] << std::endl;
 
180
    }
 
181
 
 
182
    // and free the memory again
 
183
    delete [] c_cmd;
 
184
 
 
185
    return true;
 
186
}
87
187
 
88
188
bool is_binary_in_path(const char *program)
89
189
{