~ubuntu-branches/ubuntu/oneiric/apvlv/oneiric

« back to all changes in this revision

Viewing changes to src/ApvlvUtil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ritter
  • Date: 2009-07-15 14:48:33 UTC
  • Revision ID: james.westby@ubuntu.com-20090715144833-k4hx9jqtb6vef121
Tags: upstream-0.0.6.8
ImportĀ upstreamĀ versionĀ 0.0.6.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the apvlv package
 
3
 *
 
4
 * Copyright (C) 2008 Alf.
 
5
 *
 
6
 * Contact: Alf <naihe2010@gmail.com>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License
 
10
 * as published by the Free Software Foundation; either version 2.0 of
 
11
 * the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public
 
19
 * License along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 *
 
22
 */
 
23
/* @CFILE ApvlvUtil.cpp
 
24
 *
 
25
 *  Author: Alf <naihe2010@gmail.com>
 
26
 */
 
27
/* @date Created: 2008/09/30 00:00:00 Alf */
 
28
 
 
29
#include "ApvlvUtil.hpp"
 
30
 
 
31
#include <stdlib.h>
 
32
#include <sys/stat.h>
 
33
#include <gtk/gtk.h>
 
34
 
 
35
#ifdef WIN32
 
36
# include <windows.h>
 
37
#endif
 
38
 
 
39
#include <string>
 
40
#include <iostream>
 
41
#include <fstream>
 
42
using namespace std;
 
43
 
 
44
namespace apvlv
 
45
{
 
46
 
 
47
#ifdef WIN32
 
48
  string helppdf = "~\\Startup.pdf";
 
49
  string iniexam = "~\\apvlvrc.example";
 
50
  string iconreg = "~\\reg.png";
 
51
  string icondir = "~\\dir.png";
 
52
  string iconpdf = "~\\pdf.png";
 
53
  string inifile = "~\\_apvlvrc";
 
54
  string sessionfile = "~\\_apvlvinfo";
 
55
#else
 
56
  string helppdf = string (DOCDIR) + "/Startup.pdf";
 
57
  string iniexam = string (DOCDIR) + "/apvlvrc.example";
 
58
  string iconreg = string (DOCDIR) + "/reg.png";
 
59
  string icondir = string (DOCDIR) + "/dir.png";
 
60
  string iconpdf = string (DOCDIR) + "/pdf.png";
 
61
  string inifile = "~/.apvlvrc";
 
62
  string sessionfile = "~/.apvlvinfo";
 
63
#endif
 
64
 
 
65
  // Converts the path given to a absolute path.
 
66
  // Warning: The string is returned a new allocated buffer, NEED TO BE g_free
 
67
  char *
 
68
    absolutepath (const char *path)
 
69
      {
 
70
#ifndef PATH_MAX
 
71
#define PATH_MAX 4096
 
72
#endif
 
73
        char abpath[PATH_MAX];
 
74
 
 
75
 
 
76
        if (g_path_is_absolute (path))
 
77
          {
 
78
            return g_strdup (path);
 
79
          }
 
80
 
 
81
        if (*path == '~')
 
82
          {
 
83
#ifdef WIN32
 
84
            gchar *home = g_win32_get_package_installation_directory_of_module (NULL);
 
85
#else
 
86
            char *home = getenv ("HOME");
 
87
#endif
 
88
            g_snprintf (abpath, sizeof abpath, "%s%s",
 
89
                        home,
 
90
                        ++ path);
 
91
          }
 
92
        else
 
93
          {
 
94
#ifdef WIN32
 
95
            GetCurrentDirectoryA (sizeof abpath, abpath);
 
96
            strcat (abpath, "\\");
 
97
            strcat (abpath, path);
 
98
#else
 
99
            realpath (path, abpath);
 
100
#endif
 
101
          }
 
102
 
 
103
        return g_strdup (abpath);
 
104
      }
 
105
 
 
106
  // Copy a file
 
107
  bool
 
108
    filecpy (const char *dst, const char *src)
 
109
      {
 
110
        gchar *content;
 
111
        gchar *s = absolutepath (src);
 
112
        gchar *d = absolutepath (dst);
 
113
        bool ok = false;
 
114
 
 
115
        gboolean ret = g_file_get_contents (s, &content, NULL, NULL);
 
116
        if (ret == TRUE)
 
117
          {
 
118
            ret = g_file_set_contents (d, content, -1, NULL);
 
119
            g_free (content);
 
120
            ok = ret;
 
121
          }
 
122
 
 
123
        g_free (s);
 
124
        g_free (d);
 
125
 
 
126
        return ok;
 
127
      }
 
128
 
 
129
  // replace a widget with a new widget
 
130
  // return the parent widget
 
131
  GtkWidget *
 
132
    replace_widget (GtkWidget *owid, GtkWidget *nwid)
 
133
      {
 
134
        GtkWidget *parent = gtk_widget_get_parent (owid);
 
135
        debug ("parent: %p, owid: %p, nwid: %p", parent, owid, nwid);
 
136
        gtk_container_remove (GTK_CONTAINER (parent), owid);
 
137
        gtk_container_add (GTK_CONTAINER (parent), nwid);
 
138
        gtk_widget_show_all (parent);
 
139
        return parent;
 
140
      }
 
141
 
 
142
  // get a PopplerDocument from a given file
 
143
  // return the pointer
 
144
  PopplerDocument *
 
145
    file_to_popplerdoc (const char *filename)
 
146
      {
 
147
        static gchar *mRawdata = NULL;
 
148
        static guint mRawdatasize = 0;
 
149
        gchar *wfilename;
 
150
 
 
151
        if (filename == NULL
 
152
            || *filename == '\0'
 
153
            || g_file_test (filename, G_FILE_TEST_IS_REGULAR) == FALSE
 
154
            || (wfilename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL)) == NULL
 
155
        )
 
156
          {
 
157
            errp ("filename error: %s", filename? filename: "No name");
 
158
            return NULL;
 
159
          }
 
160
 
 
161
        size_t filelen;
 
162
        struct stat sbuf;
 
163
        int rt = stat (wfilename, &sbuf);
 
164
        if (rt < 0)
 
165
          {
 
166
            errp ("Can't stat the PDF file: %s.", filename);
 
167
            return NULL;
 
168
          }
 
169
        filelen = sbuf.st_size;
 
170
 
 
171
        if (mRawdata != NULL
 
172
            && mRawdatasize < filelen)
 
173
          {
 
174
            delete []mRawdata;
 
175
            mRawdata = NULL;
 
176
          }
 
177
 
 
178
        if (mRawdata == NULL)
 
179
          {
 
180
            mRawdata = new char[filelen];
 
181
            mRawdatasize = filelen;
 
182
          }
 
183
 
 
184
        ifstream ifs (wfilename, ios::binary);
 
185
        if (ifs.is_open ())
 
186
          {
 
187
            ifs.read (mRawdata, filelen);
 
188
            ifs.close ();
 
189
          }
 
190
 
 
191
        g_free (wfilename);
 
192
 
 
193
        PopplerDocument *doc = poppler_document_new_from_data (mRawdata, filelen, NULL, NULL);
 
194
 
 
195
        if (doc == NULL
 
196
            //            && POPPLER_ERROR == POPPLER_ERROR_ENCRYPTED) /* fix this later */
 
197
          )
 
198
            {
 
199
              GtkWidget *dia = 
 
200
                gtk_message_dialog_new (NULL, 
 
201
                                        GTK_DIALOG_DESTROY_WITH_PARENT, 
 
202
                                        GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
 
203
                                        "Maybe this PDF file is encrypted, please input a password:");
 
204
 
 
205
              GtkWidget *entry = gtk_entry_new ();
 
206
              gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dia)->vbox), entry, TRUE, TRUE, 10);
 
207
              gtk_widget_show (entry);
 
208
 
 
209
              int ret = gtk_dialog_run (GTK_DIALOG (dia));
 
210
              if (ret == GTK_RESPONSE_OK)
 
211
                {
 
212
                  gchar *ans = (gchar *) gtk_entry_get_text (GTK_ENTRY (entry));
 
213
                  if (ans != NULL)
 
214
                    {
 
215
                      doc = poppler_document_new_from_data (mRawdata, filelen, ans, NULL);
 
216
                    }
 
217
                }
 
218
 
 
219
              gtk_widget_destroy (dia);
 
220
            }
 
221
 
 
222
        return doc;
 
223
      }
 
224
 
 
225
  void
 
226
    logv (const char *level, const char *file, int line, const char *func, const char *ms, ...)
 
227
      {
 
228
        char p[0x1000], temp[0x100];
 
229
        va_list vap;
 
230
 
 
231
        g_snprintf (temp, sizeof temp, "[%s] %s: %d: %s(): ",
 
232
                    level, file, line, func);
 
233
 
 
234
        va_start (vap, ms);
 
235
        vsnprintf (p, sizeof p, ms, vap);
 
236
        va_end (vap);
 
237
 
 
238
        cerr << temp << p << endl;
 
239
      }
 
240
}