root/trunk/src/JobLoad.cxx

Revision 222, 6.0 kB (checked in by jordi, 2 years ago)

Added a new class named JobPrint? whose duty is to render the current document to PostScript? and print the resultant file. It already renders the requested pages (Note: the setUpPageRange is horrible) to PostScript?.

Line 
1// ePDFView - A lightweight PDF Viewer.
2// Copyright (C) 2006 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
21using namespace ePDFView;
22
23// Forward declarations.
24static gboolean job_load_done (gpointer data);
25static gboolean job_load_error (gpointer data);
26static gboolean job_load_password (gpointer data);
27static gboolean job_reload_done (gpointer data);
28
29///
30/// @brief Constructs a new JobLoad object.
31///
32JobLoad::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///
45JobLoad::~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///
60IDocument &
61JobLoad::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///
73GError *
74JobLoad::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///
84const gchar *
85JobLoad::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///
95const gchar *
96JobLoad::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///
106gboolean
107JobLoad::isReloading ()
108{
109    return m_Reload;
110}
111
112///
113/// @brief Opens the document.
114///
115gboolean
116JobLoad::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///
151void
152JobLoad::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///
164void
165JobLoad::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///
179void
180JobLoad::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///
191void
192JobLoad::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///
203void
204JobLoad::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///
218gboolean
219job_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///
238gboolean
239job_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->getError ());
245    JOB_NOTIFIER_END();
246
247    return FALSE;
248}
249
250///
251/// @brief The document is encrypted.
252///
253/// This function is called when the document is encrypted. It asks to the
254/// user the password and tries until the user doesn't supply a password
255/// anymore (returned NULL) or canTryPassword() returns FALSE.
256///
257/// When trying a new password, it creates a new thread.
258///
259/// @param data This parameter hold the JobLoad to open.
260///
261gboolean
262job_load_password (gpointer data)
263{
264    g_assert (NULL != data && "The data parameter is NULL.");
265
266    JobLoad *job = (JobLoad *)data;
267    job->getDocument ().notifyLoadPassword (job->getFileName (),
268                                            job->isReloading (),
269                                            job->getError ());
270    JOB_NOTIFIER_END();
271
272    return FALSE;
273}
274
275///
276/// @brief The reload is finished correctly.
277///
278/// @param data This parameter holds the JobLoad that finished.
279///
280gboolean
281job_reload_done (gpointer data)
282{
283    g_assert (NULL != data && "The data parameter is NULL.");
284
285    JobLoad *job = (JobLoad *)data;
286    job->getDocument ().notifyReload ();
287    JOB_NOTIFIER_END();
288
289    return FALSE;
290}
Note: See TracBrowser for help on using the browser.