]> www.fi.muni.cz Git - evince.git/blob - shell/ev-job-xfer.c
49f674e53f2ebdbeec8b9c6840426aec05c70bef
[evince.git] / shell / ev-job-xfer.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2005 Red Hat, Inc
4  *
5  * Evince is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Evince is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24
25 #include "ev-job-xfer.h"
26 #include "ev-document-types.h"
27 #include "ev-file-helpers.h"
28
29 #include <glib/gi18n.h>
30 #include <glib.h>
31
32 #include <libgnomevfs/gnome-vfs-uri.h>
33 #include <libgnomevfs/gnome-vfs-utils.h>
34 #include <libgnomevfs/gnome-vfs-ops.h>
35 #include <libgnomevfs/gnome-vfs-xfer.h>
36
37 static void ev_job_xfer_init            (EvJobXfer           *job);
38 static void ev_job_xfer_class_init      (EvJobXferClass      *class);
39
40 G_DEFINE_TYPE (EvJobXfer, ev_job_xfer, EV_TYPE_JOB)
41
42 static void ev_job_xfer_init (EvJobXfer *job) { /* Do Nothing */ }
43
44 static void
45 ev_job_xfer_dispose (GObject *object)
46 {
47         EvJobXfer *job = EV_JOB_XFER (object);
48
49         if (job->uri) {
50                 g_free (job->uri);
51                 job->uri = NULL;
52         }
53
54         if (job->local_uri) {
55                 g_free (job->local_uri);
56                 job->local_uri = NULL;
57         }
58
59         if (job->error) {
60                 g_error_free (job->error);
61                 job->error = NULL;
62         }
63
64         (* G_OBJECT_CLASS (ev_job_xfer_parent_class)->dispose) (object);
65 }
66
67 static void
68 ev_job_xfer_class_init (EvJobXferClass *class)
69 {
70         GObjectClass *oclass;
71
72         oclass = G_OBJECT_CLASS (class);
73
74         oclass->dispose = ev_job_xfer_dispose;
75 }
76
77
78 EvJob *
79 ev_job_xfer_new (const gchar *uri)
80 {
81         EvJobXfer *job;
82
83         job = g_object_new (EV_TYPE_JOB_XFER, NULL);
84
85         job->uri = g_strdup (uri);
86
87         return EV_JOB (job);
88 }
89
90 void
91 ev_job_xfer_run (EvJobXfer *job)
92 {
93         GType document_type;
94         GError *error = NULL;
95         GnomeVFSURI *source_uri;
96         GnomeVFSURI *target_uri;
97
98         g_return_if_fail (EV_IS_JOB_XFER (job));
99         
100         if (job->error) {
101                 g_error_free (job->error);
102                 job->error = NULL;
103         }
104
105         document_type = ev_document_type_lookup (job->uri, NULL, &error);
106
107         if (document_type != G_TYPE_INVALID) {
108                 EV_JOB (job)->document = g_object_new (document_type, NULL);
109         } else {
110                 job->error = error;                     
111                 EV_JOB (job)->finished = TRUE;
112                 return; 
113         }
114         
115         source_uri = gnome_vfs_uri_new (job->uri);
116         if (!gnome_vfs_uri_is_local (source_uri)) {
117                 char *tmp_name;
118                 
119                 tmp_name = ev_tmp_filename ();
120                 job->local_uri = g_strconcat ("file:", tmp_name, NULL);
121                 g_free (tmp_name);
122                 
123                 target_uri = gnome_vfs_uri_new (job->local_uri);
124
125                 gnome_vfs_xfer_uri (source_uri, target_uri, 
126                                     GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
127                                     GNOME_VFS_XFER_ERROR_MODE_ABORT,
128                                     GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
129                                     NULL,
130                                     job);
131                 gnome_vfs_uri_unref (target_uri);
132         }
133         gnome_vfs_uri_unref (source_uri);
134
135         EV_JOB (job)->finished = TRUE;
136         return;
137 }
138
139