1 // ePDFView - A lightweight PDF Viewer.
2 // Copyright (C) 2006, 2007, 2009 Emma's Software.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 
18 #include <config.h>
19 #include "epdfview.h"
20 
21 using namespace ePDFView;
22 
23 // Forward declarations.
24 static gboolean job_load_done (gpointer data);
25 static gboolean job_load_error (gpointer data);
26 static gboolean job_load_password (gpointer data);
27 static gboolean job_reload_done (gpointer data);
28 
29 ///
30 /// @brief Constructs a new JobLoad object.
31 ///
JobLoad()32 JobLoad::JobLoad ():
33     IJob ()
34 {
35     m_Document = NULL;
36     m_Error = NULL;
37     m_FileName = NULL;
38     m_Password = NULL;
39     m_Reload = FALSE;
40 }
41 
42 ///
43 /// @brief Deletes all dynamically allocated memory by JobLoad.
44 ///
~JobLoad()45 JobLoad::~JobLoad ()
46 {
47     g_free (m_FileName);
48     if ( NULL != m_Error )
49     {
50         g_error_free (m_Error);
51     }
52     g_free (m_Password);
53 }
54 
55 ///
56 /// @brief The document that's loading.
57 ///
58 /// @return The document that is loading.
59 ///
60 IDocument &
getDocument()61 JobLoad::getDocument ()
62 {
63     g_assert (NULL != m_Document && "The document is NULL.");
64 
65     return *m_Document;
66 }
67 
68 ///
69 /// @brief Gets the last error.
70 ///
71 /// @return The last load error.
72 ///
73 GError *
getError()74 JobLoad::getError ()
75 {
76     return m_Error;
77 }
78 
79 ///
80 /// @brief Gets the file name to load or reload.
81 ///
82 /// @return The file name to load or reload.
83 ///
84 const gchar *
getFileName()85 JobLoad::getFileName ()
86 {
87     return m_FileName;
88 }
89 
90 ///
91 /// @brief Gets the password.
92 ///
93 /// @return The password to use to open the document.
94 ///
95 const gchar *
getPassword()96 JobLoad::getPassword ()
97 {
98     return m_Password;
99 }
100 
101 ///
102 /// @brief Check if we are reloading.
103 ///
104 /// @return TRUE if are reloading, FALSE otherwise.
105 ///
106 gboolean
isReloading()107 JobLoad::isReloading ()
108 {
109     return m_Reload;
110 }
111 
112 ///
113 /// @brief Opens the document.
114 ///
115 gboolean
run()116 JobLoad::run ()
117 {
118     GError *error = NULL;
119     if ( getDocument ().loadFile (getFileName (), getPassword (), &error) )
120     {
121         if ( isReloading () )
122         {
123             JOB_NOTIFIER (job_reload_done, this);
124         }
125         else
126         {
127             JOB_NOTIFIER (job_load_done, this);
128         }
129     }
130     else
131     {
132         setError (error);
133         if ( DocumentErrorEncrypted == error->code )
134         {
135             JOB_NOTIFIER (job_load_password, this);
136         }
137         else
138         {
139             JOB_NOTIFIER (job_load_error, this);
140         }
141     }
142 
143     return JOB_DELETE;
144 }
145 
146 ///
147 /// @brief Sets the document to load.
148 ///
149 /// @param document The document that will load.
150 ///
151 void
setDocument(IDocument * document)152 JobLoad::setDocument (IDocument *document)
153 {
154     g_assert ( NULL != document && "Tried to set a NULL document.");
155 
156     m_Document = document;
157 }
158 
159 ///
160 /// @brief Sets the last error.
161 ///
162 /// @param error The last error opening a file.
163 ///
164 void
setError(GError * error)165 JobLoad::setError (GError *error)
166 {
167     if ( NULL != m_Error )
168     {
169         g_error_free (m_Error);
170     }
171     m_Error = error;
172 }
173 
174 ///
175 /// @brief Sets the file name to open.
176 ///
177 /// @param fileName The file name to load or reload.
178 ///
179 void
setFileName(const gchar * fileName)180 JobLoad::setFileName (const gchar *fileName)
181 {
182     g_free (m_FileName);
183     m_FileName = g_strdup (fileName);
184 }
185 
186 ///
187 /// @brief Sets the password to open the document.
188 ///
189 /// @param password The password to use.
190 ///
191 void
setPassword(const gchar * password)192 JobLoad::setPassword (const gchar *password)
193 {
194     g_free (m_Password);
195     m_Password = g_strdup (password);
196 }
197 
198 ///
199 /// @brief Sets if reload.
200 ///
201 /// @param reload TRUE if must reload, FALSE otherwise.
202 ///
203 void
setReload(gboolean reload)204 JobLoad::setReload (gboolean reload)
205 {
206     m_Reload = reload;
207 }
208 
209 ////////////////////////////////////////////////////////////////
210 // Static threaded functions.
211 ////////////////////////////////////////////////////////////////
212 
213 ///
214 /// @brief The load is finished correctly.
215 ///
216 /// @param data This parameter holds the JobLoad that finished.
217 ///
218 gboolean
job_load_done(gpointer data)219 job_load_done (gpointer data)
220 {
221     g_assert (NULL != data && "The data parameter is NULL.");
222 
223     JobLoad *job = (JobLoad *)data;
224     job->getDocument ().notifyLoad ();
225     JOB_NOTIFIER_END();
226 
227     return FALSE;
228 }
229 
230 ///
231 /// @brief The load had errors.
232 ///
233 /// This is called when the load of the document had any error except
234 /// when it's encrypted.
235 ///
236 /// @param data This parameter hold the JobLoad to open.
237 ///
238 gboolean
job_load_error(gpointer data)239 job_load_error (gpointer data)
240 {
241     g_assert (NULL != data && "The data parameter is NULL.");
242 
243     JobLoad *job = (JobLoad *)data;
244     job->getDocument ().notifyLoadError (job->getFileName(),
245             job->getError ());
246     JOB_NOTIFIER_END();
247 
248     return FALSE;
249 }
250 
251 ///
252 /// @brief The document is encrypted.
253 ///
254 /// This function is called when the document is encrypted. It asks to the
255 /// user the password and tries until the user doesn't supply a password
256 /// anymore (returned NULL) or canTryPassword() returns FALSE.
257 ///
258 /// When trying a new password, it creates a new thread.
259 ///
260 /// @param data This parameter hold the JobLoad to open.
261 ///
262 gboolean
job_load_password(gpointer data)263 job_load_password (gpointer data)
264 {
265     g_assert (NULL != data && "The data parameter is NULL.");
266 
267     JobLoad *job = (JobLoad *)data;
268     job->getDocument ().notifyLoadPassword (job->getFileName (),
269                                             job->isReloading (),
270                                             job->getError ());
271     JOB_NOTIFIER_END();
272 
273     return FALSE;
274 }
275 
276 ///
277 /// @brief The reload is finished correctly.
278 ///
279 /// @param data This parameter holds the JobLoad that finished.
280 ///
281 gboolean
job_reload_done(gpointer data)282 job_reload_done (gpointer data)
283 {
284     g_assert (NULL != data && "The data parameter is NULL.");
285 
286     JobLoad *job = (JobLoad *)data;
287     job->getDocument ().notifyReload ();
288     JOB_NOTIFIER_END();
289 
290     return FALSE;
291 }
292