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_save_done (gpointer data);
25 static gboolean job_save_error (gpointer data);
26 
27 ///
28 /// @brief Constructs a new JobSave object.
29 ///
JobSave()30 JobSave::JobSave ():
31     IJob ()
32 {
33     m_Document = NULL;
34     m_Error = NULL;
35     m_FileName = NULL;
36 }
37 
38 ///
39 /// @brief Deletes all dynamically allocated memory by JobSave.
40 ///
~JobSave()41 JobSave::~JobSave ()
42 {
43     g_free (m_FileName);
44     if ( NULL != m_Error )
45     {
46         g_error_free (m_Error);
47     }
48 }
49 
50 ///
51 /// @brief The document that's saving a copy from.
52 ///
53 /// @return The document that is saving a copy from.
54 ///
55 IDocument &
getDocument()56 JobSave::getDocument ()
57 {
58     g_assert (NULL != m_Document && "The document is NULL.");
59 
60     return *m_Document;
61 }
62 
63 ///
64 /// @brief Gets the last error.
65 ///
66 /// @return The last save error.
67 ///
68 GError *
getError()69 JobSave::getError ()
70 {
71     return m_Error;
72 }
73 
74 ///
75 /// @brief Gets the file name to save to.
76 ///
77 /// @return The file name to save to.
78 ///
79 const gchar *
getFileName()80 JobSave::getFileName ()
81 {
82     return m_FileName;
83 }
84 
85 ///
86 /// @brief Saves a copy of the document.
87 ///
88 gboolean
run()89 JobSave::run ()
90 {
91     GError *error = NULL;
92     if ( getDocument ().saveFile (getFileName (), &error) )
93     {
94         JOB_NOTIFIER (job_save_done, this);
95     }
96     else
97     {
98         setError (error);
99         JOB_NOTIFIER (job_save_error, this);
100     }
101 
102     return JOB_DELETE;
103 }
104 
105 ///
106 /// @brief Sets the document to save a copy from.
107 ///
108 /// @param document The document that will be saved.
109 ///
110 void
setDocument(IDocument * document)111 JobSave::setDocument (IDocument *document)
112 {
113     g_assert ( NULL != document && "Tried to set a NULL document.");
114 
115     m_Document = document;
116 }
117 
118 ///
119 /// @brief Sets the last error.
120 ///
121 /// @param error The last error saving a file.
122 ///
123 void
setError(GError * error)124 JobSave::setError (GError *error)
125 {
126     if ( NULL != m_Error )
127     {
128         g_error_free (m_Error);
129     }
130     m_Error = error;
131 }
132 
133 ///
134 /// @brief Sets the file name to save to.
135 ///
136 /// @param fileName The file name to save to.
137 ///
138 void
setFileName(const gchar * fileName)139 JobSave::setFileName (const gchar *fileName)
140 {
141     g_free (m_FileName);
142     m_FileName = g_strdup (fileName);
143 }
144 
145 ////////////////////////////////////////////////////////////////
146 // Static threaded functions.
147 ////////////////////////////////////////////////////////////////
148 
149 ///
150 /// @brief The save has finished correctly.
151 ///
152 /// @param data This parameter holds the JobSave that finished.
153 ///
154 gboolean
job_save_done(gpointer data)155 job_save_done (gpointer data)
156 {
157     g_assert (NULL != data && "The data parameter is NULL.");
158 
159     JobSave *job = (JobSave *)data;
160     job->getDocument ().notifySave ();
161     JOB_NOTIFIER_END();
162 
163     return FALSE;
164 }
165 
166 ///
167 /// @brief The save process had errors.
168 ///
169 /// This is called when the save of the document's copy had any error.
170 ///
171 /// @param data This parameter holds the JobSave that failed.
172 ///
173 gboolean
job_save_error(gpointer data)174 job_save_error (gpointer data)
175 {
176     g_assert (NULL != data && "The data parameter is NULL.");
177 
178     JobSave *job = (JobSave *)data;
179     job->getDocument ().notifySaveError (job->getError ());
180     JOB_NOTIFIER_END();
181 
182     return FALSE;
183 }
184