updated api documentation
[urpo] / www / apidoc / html / printjob_8cpp_source.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5 <title>Urpo: src/printjob.cpp Source File</title>
6 <link href="tabs.css" rel="stylesheet" type="text/css"/>
7 <link href="doxygen.css" rel="stylesheet" type="text/css"/>
8 </head>
9 <body>
10 <!-- Generated by Doxygen 1.6.3 -->
11 <div class="navigation" id="top">
12   <div class="tabs">
13     <ul>
14       <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
15       <li><a href="annotated.html"><span>Classes</span></a></li>
16       <li class="current"><a href="files.html"><span>Files</span></a></li>
17     </ul>
18   </div>
19   <div class="tabs">
20     <ul>
21       <li><a href="files.html"><span>File&nbsp;List</span></a></li>
22     </ul>
23   </div>
24 <h1>src/printjob.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/**************************************************************************</span>
25 <a name="l00002"></a>00002 <span class="comment"></span>
26 <a name="l00003"></a>00003 <span class="comment">    URPO</span>
27 <a name="l00004"></a>00004 <span class="comment"></span>
28 <a name="l00005"></a>00005 <span class="comment">    Unix Remote Printing Operation</span>
29 <a name="l00006"></a>00006 <span class="comment">    Copyright (c) Arto Hyvättinen 2010</span>
30 <a name="l00007"></a>00007 <span class="comment"></span>
31 <a name="l00008"></a>00008 <span class="comment">    This file is part of URPO.</span>
32 <a name="l00009"></a>00009 <span class="comment"></span>
33 <a name="l00010"></a>00010 <span class="comment">    URPO is free software: you can redistribute it and/or modify</span>
34 <a name="l00011"></a>00011 <span class="comment">    it under the terms of the GNU General Public License as published by</span>
35 <a name="l00012"></a>00012 <span class="comment">    the Free Software Foundation, either version 3 of the License, or</span>
36 <a name="l00013"></a>00013 <span class="comment">    (at your option) any later version.</span>
37 <a name="l00014"></a>00014 <span class="comment"></span>
38 <a name="l00015"></a>00015 <span class="comment">    URPO is distributed in the hope that it will be useful,</span>
39 <a name="l00016"></a>00016 <span class="comment">    but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
40 <a name="l00017"></a>00017 <span class="comment">    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
41 <a name="l00018"></a>00018 <span class="comment">    GNU General Public License for more details.</span>
42 <a name="l00019"></a>00019 <span class="comment"></span>
43 <a name="l00020"></a>00020 <span class="comment"></span>
44 <a name="l00021"></a>00021 <span class="comment">**************************************************************************/</span>
45 <a name="l00022"></a>00022 
46 <a name="l00023"></a>00023 <span class="preprocessor">#include &quot;printjob.h&quot;</span>
47 <a name="l00024"></a>00024 <span class="preprocessor">#include &quot;urpoconnection.h&quot;</span>
48 <a name="l00025"></a>00025 <span class="preprocessor">#include &lt;QFileInfo&gt;</span>
49 <a name="l00026"></a>00026 <span class="preprocessor">#include &lt;stdexcept&gt;</span> <span class="comment">// Thowing execption for start()</span>
50 <a name="l00027"></a>00027 <span class="preprocessor">#include &lt;cstdlib&gt;</span>  <span class="comment">// For random numbers generating temp file name</span>
51 <a name="l00028"></a>00028 
52 <a name="l00029"></a>00029 PrintJob::PrintJob(<a class="code" href="class_urpo_connection.html" title="Information about ssh connection (abstract).">UrpoConnection</a>* connection) :
53 <a name="l00030"></a>00030     <a class="code" href="class_urpo_job.html" title="Base class for jobs (abstract).">UrpoJob</a>(connection)
54 <a name="l00031"></a>00031 {
55 <a name="l00032"></a>00032     currentProcess_ = 0;
56 <a name="l00033"></a>00033 }
57 <a name="l00034"></a>00034 
58 <a name="l00035"></a>00035 PrintJob::~PrintJob()
59 <a name="l00036"></a>00036 {
60 <a name="l00037"></a>00037     <span class="keywordflow">if</span>( currentProcess_ )
61 <a name="l00038"></a>00038         <span class="keyword">delete</span> currentProcess_;
62 <a name="l00039"></a>00039 }
63 <a name="l00040"></a>00040 
64 <a name="l00041"></a>00041 
65 <a name="l00042"></a>00042 <span class="keywordtype">void</span> PrintJob::freeProcess()
66 <a name="l00043"></a>00043 {
67 <a name="l00044"></a>00044     <span class="keywordflow">if</span>( currentProcess_)
68 <a name="l00045"></a>00045     {
69 <a name="l00046"></a>00046         <span class="comment">// Delete current process when event loop</span>
70 <a name="l00047"></a>00047         <span class="comment">//</span>
71 <a name="l00048"></a>00048         <span class="comment">// If we are processing a signal from this process</span>
72 <a name="l00049"></a>00049         <span class="comment">// (process is finished), we can&#39;t delete process</span>
73 <a name="l00050"></a>00050         <span class="comment">// (actually, we can delete, but we get null pointer</span>
74 <a name="l00051"></a>00051         <span class="comment">// and segmentation fault !)</span>
75 <a name="l00052"></a>00052         currentProcess_-&gt;deleteLater();
76 <a name="l00053"></a>00053 
77 <a name="l00054"></a>00054         <span class="comment">// disvalidate pointer</span>
78 <a name="l00055"></a>00055         currentProcess_ = 0;
79 <a name="l00056"></a>00056     }
80 <a name="l00057"></a>00057 }
81 <a name="l00058"></a>00058 
82 <a name="l00059"></a><a class="code" href="class_print_job.html#a167ee653ddcb9f5130b9f92fce279050">00059</a> <span class="keywordtype">void</span> <a class="code" href="class_print_job.html#a167ee653ddcb9f5130b9f92fce279050" title="Do not use, thows execption!">PrintJob::startJob</a>()
83 <a name="l00060"></a>00060 {
84 <a name="l00061"></a>00061     <span class="comment">// See API documentation on header...</span>
85 <a name="l00062"></a>00062     <span class="keywordflow">throw</span>( <span class="keyword">new</span> std::logic_error(<span class="stringliteral">&quot;PrintJob::starJob() NOT for use!&quot;</span>) );
86 <a name="l00063"></a>00063 }
87 <a name="l00064"></a>00064 
88 <a name="l00065"></a>00065 
89 <a name="l00066"></a><a class="code" href="class_print_job.html#a5ce2230a6599b03abefd3875a43ba62e">00066</a> <span class="keywordtype">void</span> <a class="code" href="class_print_job.html#a5ce2230a6599b03abefd3875a43ba62e" title="Print sigle file.">PrintJob::printFile</a>(<span class="keyword">const</span> QString&amp; path, <span class="keyword">const</span> QString&amp; printOptions)
90 <a name="l00067"></a>00067 {
91 <a name="l00068"></a>00068     <span class="comment">// Get information about file</span>
92 <a name="l00069"></a>00069     QFileInfo fileInfo(path);
93 <a name="l00070"></a>00070     <span class="keywordflow">if</span>( fileInfo.exists() == <span class="keyword">false</span>)
94 <a name="l00071"></a>00071     {
95 <a name="l00072"></a>00072         <span class="comment">// File not found!</span>
96 <a name="l00073"></a>00073         <a class="code" href="class_urpo_job.html#a6105276a7d9c7431aa5f24d374e33f84" title="Send debug message.">sendDebugMessage</a>( tr(<span class="stringliteral">&quot;File %1 not exists.&quot;</span>).arg(path));
97 <a name="l00074"></a>00074         <a class="code" href="class_urpo_job.html#a79d4688e55e26b373e103cb7238df9b6" title="Finish failed job.">fail</a>( tr(<span class="stringliteral">&quot;File not found&quot;</span>) );
98 <a name="l00075"></a>00075         <span class="keywordflow">return</span>;
99 <a name="l00076"></a>00076     }
100 <a name="l00077"></a>00077 
101 <a name="l00078"></a>00078     <span class="comment">// new process for use</span>
102 <a name="l00079"></a>00079     freeProcess();
103 <a name="l00080"></a>00080     currentProcess_= <a class="code" href="class_urpo_job.html#ab54ee3b2ba06bfc71c310c746a846c48" title="Create new UrpoProcess.">newProcess</a>();
104 <a name="l00081"></a>00081 
105 <a name="l00082"></a>00082 
106 <a name="l00083"></a>00083     <span class="comment">// Copy temperary file to temperary directory</span>
107 <a name="l00084"></a>00084     pathToPrint_ = <span class="stringliteral">&quot;/var/tmp/urpo-&quot;</span>;
108 <a name="l00085"></a>00085 
109 <a name="l00086"></a>00086     <span class="comment">// Securing unique of temperary files, add random</span>
110 <a name="l00087"></a>00087     <span class="comment">// number between 0 and ffffff</span>
111 <a name="l00088"></a>00088     pathToPrint_.append( QString::number( rand() % 0xffffff, 16));
112 <a name="l00089"></a>00089 
113 <a name="l00090"></a>00090     <span class="comment">// In server path, replace space with _</span>
114 <a name="l00091"></a>00091     pathToPrint_.append( fileInfo.fileName().replace(QChar(<span class="charliteral">&#39; &#39;</span>),QChar(<span class="charliteral">&#39;_&#39;</span>)));
115 <a name="l00092"></a>00092 
116 <a name="l00093"></a>00093     printOptions_ = printOptions;
117 <a name="l00094"></a>00094 
118 <a name="l00095"></a>00095     <span class="comment">// Copy command</span>
119 <a name="l00096"></a>00096     QString command = <span class="stringliteral">&quot;scp &quot;</span>;
120 <a name="l00097"></a>00097     command.append( <a class="code" href="class_urpo_job.html#a5c06ededd10c78ca736df20e1df1e269" title="Get connection pointer.">getConnection</a>()-&gt;getKeyOption());
121 <a name="l00098"></a>00098     command.append(fileInfo.absoluteFilePath().replace(QString(<span class="stringliteral">&quot; &quot;</span>),QString(<span class="stringliteral">&quot;\\ &quot;</span>)) );
122 <a name="l00099"></a>00099     command.append(<span class="stringliteral">&quot; &quot;</span>);
123 <a name="l00100"></a>00100     command.append( <a class="code" href="class_urpo_job.html#a5c06ededd10c78ca736df20e1df1e269" title="Get connection pointer.">getConnection</a>()-&gt;getHostString() );
124 <a name="l00101"></a>00101     command.append(<span class="stringliteral">&quot;:&quot;</span>);
125 <a name="l00102"></a>00102     command.append(pathToPrint_);
126 <a name="l00103"></a>00103 
127 <a name="l00104"></a>00104     <span class="comment">// Next Copy Ready</span>
128 <a name="l00105"></a>00105     connect( currentProcess_, SIGNAL(<a class="code" href="class_urpo_job.html#ab931a6a72a6f49c8919dcd695197288e" title="Job finished (successed of failed).">finished</a>(<span class="keywordtype">bool</span>)), <span class="keyword">this</span>, SLOT(<a class="code" href="class_print_job.html#a2dacf12d369e28e30a5955aba14d4fcf" title="When file has copied (internal use only).">copyReady</a>(<span class="keywordtype">bool</span>)));
129 <a name="l00106"></a>00106 
130 <a name="l00107"></a>00107     currentProcess_-&gt;<a class="code" href="class_urpo_process.html#a1bf2519b81fe6d5074db04f9acd38cb0" title="Run command.">start</a>(command);
131 <a name="l00108"></a>00108 }
132 <a name="l00109"></a>00109 
133 <a name="l00110"></a><a class="code" href="class_print_job.html#a2dacf12d369e28e30a5955aba14d4fcf">00110</a> <span class="keywordtype">void</span> <a class="code" href="class_print_job.html#a2dacf12d369e28e30a5955aba14d4fcf" title="When file has copied (internal use only).">PrintJob::copyReady</a>(<span class="keywordtype">bool</span> success)
134 <a name="l00111"></a>00111 {
135 <a name="l00112"></a>00112     freeProcess();
136 <a name="l00113"></a>00113 
137 <a name="l00114"></a>00114     <span class="keywordflow">if</span>( success == <span class="keyword">false</span>)
138 <a name="l00115"></a>00115     {
139 <a name="l00116"></a>00116         <a class="code" href="class_urpo_job.html#a79d4688e55e26b373e103cb7238df9b6" title="Finish failed job.">fail</a>( tr(<span class="stringliteral">&quot;Failed copying file&quot;</span>));
140 <a name="l00117"></a>00117         <span class="keywordflow">return</span>;
141 <a name="l00118"></a>00118     }
142 <a name="l00119"></a>00119 
143 <a name="l00120"></a>00120     <span class="comment">// Running remote printing command</span>
144 <a name="l00121"></a>00121     currentProcess_ = <a class="code" href="class_urpo_job.html#ab54ee3b2ba06bfc71c310c746a846c48" title="Create new UrpoProcess.">newProcess</a>();
145 <a name="l00122"></a>00122     QString command = <span class="stringliteral">&quot; ssh &quot;</span>;
146 <a name="l00123"></a>00123     command.append( <a class="code" href="class_urpo_job.html#a5c06ededd10c78ca736df20e1df1e269" title="Get connection pointer.">getConnection</a>()-&gt;getKeyOption()) ;
147 <a name="l00124"></a>00124     command.append(<a class="code" href="class_urpo_job.html#a5c06ededd10c78ca736df20e1df1e269" title="Get connection pointer.">getConnection</a>()-&gt;getHostString());
148 <a name="l00125"></a>00125     command.append(<span class="stringliteral">&quot; lp &quot;</span>);
149 <a name="l00126"></a>00126     command.append(printOptions_);
150 <a name="l00127"></a>00127     command.append(<span class="stringliteral">&quot; &quot;</span>);
151 <a name="l00128"></a>00128     command.append(pathToPrint_);
152 <a name="l00129"></a>00129 
153 <a name="l00130"></a>00130     <span class="comment">// Next print ready</span>
154 <a name="l00131"></a>00131     connect( currentProcess_, SIGNAL(<a class="code" href="class_urpo_job.html#ab931a6a72a6f49c8919dcd695197288e" title="Job finished (successed of failed).">finished</a>(<span class="keywordtype">bool</span>)), <span class="keyword">this</span>, SLOT(<a class="code" href="class_print_job.html#af984397994fd563b0a0a810b77a68c2c" title="When files has printed (internal use only).">printReady</a>(<span class="keywordtype">bool</span>)));
155 <a name="l00132"></a>00132     currentProcess_-&gt;<a class="code" href="class_urpo_process.html#a1bf2519b81fe6d5074db04f9acd38cb0" title="Run command.">start</a>(command);
156 <a name="l00133"></a>00133 
157 <a name="l00134"></a>00134 }
158 <a name="l00135"></a>00135 
159 <a name="l00136"></a><a class="code" href="class_print_job.html#af984397994fd563b0a0a810b77a68c2c">00136</a> <span class="keywordtype">void</span> <a class="code" href="class_print_job.html#af984397994fd563b0a0a810b77a68c2c" title="When files has printed (internal use only).">PrintJob::printReady</a>(<span class="keywordtype">bool</span> success)
160 <a name="l00137"></a>00137 {
161 <a name="l00138"></a>00138     <span class="comment">// Print step is ready</span>
162 <a name="l00139"></a>00139     freeProcess();
163 <a name="l00140"></a>00140 
164 <a name="l00141"></a>00141     <span class="keywordflow">if</span>( success == <span class="keyword">false</span> )
165 <a name="l00142"></a>00142     {
166 <a name="l00143"></a>00143         <a class="code" href="class_urpo_job.html#a79d4688e55e26b373e103cb7238df9b6" title="Finish failed job.">fail</a>( tr(<span class="stringliteral">&quot;Print failed&quot;</span>));
167 <a name="l00144"></a>00144         <span class="keywordflow">return</span>;
168 <a name="l00145"></a>00145     }
169 <a name="l00146"></a>00146 
170 <a name="l00147"></a>00147     <span class="comment">// delete temperary file</span>
171 <a name="l00148"></a>00148     currentProcess_ = <a class="code" href="class_urpo_job.html#ab54ee3b2ba06bfc71c310c746a846c48" title="Create new UrpoProcess.">newProcess</a>();
172 <a name="l00149"></a>00149     QString command = <span class="stringliteral">&quot; ssh &quot;</span>;
173 <a name="l00150"></a>00150     command.append( <a class="code" href="class_urpo_job.html#a5c06ededd10c78ca736df20e1df1e269" title="Get connection pointer.">getConnection</a>()-&gt;getKeyOption()) ;
174 <a name="l00151"></a>00151     command.append(<a class="code" href="class_urpo_job.html#a5c06ededd10c78ca736df20e1df1e269" title="Get connection pointer.">getConnection</a>()-&gt;getHostString());
175 <a name="l00152"></a>00152     command.append(<span class="stringliteral">&quot; rm &quot;</span>);
176 <a name="l00153"></a>00153     command.append(pathToPrint_);
177 <a name="l00154"></a>00154 
178 <a name="l00155"></a>00155     <span class="comment">// Next delete ready</span>
179 <a name="l00156"></a>00156     connect( currentProcess_, SIGNAL(<a class="code" href="class_urpo_job.html#ab931a6a72a6f49c8919dcd695197288e" title="Job finished (successed of failed).">finished</a>(<span class="keywordtype">bool</span>)),<span class="keyword">this</span>,SLOT(<a class="code" href="class_print_job.html#adb65a2765ae36c2ceab60e5a868b54f8" title="When file has deleted (internal use only).">deleteReady</a>(<span class="keywordtype">bool</span>)));
180 <a name="l00157"></a>00157     currentProcess_-&gt;<a class="code" href="class_urpo_process.html#a1bf2519b81fe6d5074db04f9acd38cb0" title="Run command.">start</a>(command);
181 <a name="l00158"></a>00158 }
182 <a name="l00159"></a>00159 
183 <a name="l00160"></a><a class="code" href="class_print_job.html#adb65a2765ae36c2ceab60e5a868b54f8">00160</a> <span class="keywordtype">void</span> <a class="code" href="class_print_job.html#adb65a2765ae36c2ceab60e5a868b54f8" title="When file has deleted (internal use only).">PrintJob::deleteReady</a>(<span class="keywordtype">bool</span> success)
184 <a name="l00161"></a>00161 {
185 <a name="l00162"></a>00162     <span class="comment">// All done!</span>
186 <a name="l00163"></a>00163     freeProcess();
187 <a name="l00164"></a>00164     <span class="keywordflow">if</span>( success == <span class="keyword">false</span> )
188 <a name="l00165"></a>00165     {
189 <a name="l00166"></a>00166         <a class="code" href="class_urpo_job.html#a79d4688e55e26b373e103cb7238df9b6" title="Finish failed job.">fail</a>(tr(<span class="stringliteral">&quot;Failed deleting file&quot;</span>));
190 <a name="l00167"></a>00167     }
191 <a name="l00168"></a>00168     <span class="keywordflow">else</span>
192 <a name="l00169"></a>00169     {
193 <a name="l00170"></a>00170         <span class="comment">// Successed !!!</span>
194 <a name="l00171"></a>00171         <a class="code" href="class_urpo_job.html#adca3f19275978d0514229c26871e8a19" title="Finish job.">finish</a>( Successed );
195 <a name="l00172"></a>00172     }
196 <a name="l00173"></a>00173 }
197 <a name="l00174"></a>00174 
198 <a name="l00175"></a><a class="code" href="class_print_job.html#a6a7f1167cd9d660a3fa1c16276d8b6b0">00175</a> <span class="keywordtype">void</span> <a class="code" href="class_print_job.html#a6a7f1167cd9d660a3fa1c16276d8b6b0" title="Cancel printing job.">PrintJob::cancelJob</a>()
199 <a name="l00176"></a>00176 {
200 <a name="l00177"></a>00177     <span class="comment">// Cancelled by user.</span>
201 <a name="l00178"></a>00178     <span class="keywordflow">if</span>( currentProcess_ )
202 <a name="l00179"></a>00179     {
203 <a name="l00180"></a>00180         currentProcess_-&gt;<a class="code" href="class_urpo_process.html#ac4381636ee9e977dfac4d8fe53ddfeaa" title="Cancel process.">terminate</a>();
204 <a name="l00181"></a>00181         freeProcess();
205 <a name="l00182"></a>00182     }
206 <a name="l00183"></a>00183     <a class="code" href="class_urpo_job.html#adca3f19275978d0514229c26871e8a19" title="Finish job.">finish</a>( Cancelled );
207 <a name="l00184"></a>00184 }
208 <a name="l00185"></a>00185 
209 </pre></div></div>
210 <hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Sep 27 18:46:05 2010 for Urpo by&nbsp;
211 <a href="http://www.doxygen.org/index.html">
212 <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
213 </body>
214 </html>