1     /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 
31 //________________________________
32 //	my own includes
33 
34 #include <jobs/jobresult.hxx>
35 #include <jobs/jobconst.hxx>
36 #include <threadhelp/readguard.hxx>
37 #include <threadhelp/writeguard.hxx>
38 #include <general.h>
39 #include <services.h>
40 
41 //________________________________
42 //	interface includes
43 
44 //________________________________
45 //	includes of other projects
46 
47 #include <rtl/ustrbuf.hxx>
48 #include <vcl/svapp.hxx>
49 #include <comphelper/sequenceashashmap.hxx>
50 
51 //________________________________
52 //	namespace
53 
54 namespace framework{
55 
56 //________________________________
57 //	non exported const
58 
59 //________________________________
60 //	non exported definitions
61 
62 //________________________________
63 //	declarations
64 
65 //________________________________
66 /**
67     @short      standard dtor
68     @descr      It does nothing else ...
69                 but it marks this new instance as non valid!
70 */
71 JobResult::JobResult()
72     : ThreadHelpBase(&Application::GetSolarMutex())
73 {
74     // reset the flag mask!
75     // It will reset the accessible state of this object.
76     // That can be usefull if something will fail here ...
77     m_eParts = E_NOPART;
78 }
79 
80 //________________________________
81 /**
82     @short      special ctor
83     @descr      It initialize this new instance with a pure job execution result
84                 and analyze it. Doing so, we update our other members.
85 
86                 <p>
87                 It's a list of named values, packed inside this any.
88                 Following protocol is used:
89                 <p>
90                 <ul>
91                     <li>
92                         "SaveArguments" [sequence< css.beans.NamedValue >]
93                         <br>
94                         The returned list of (for this generic implementation unknown!)
95                         properties, will be written directly to the configuration and replace
96                         any old values there. There will no check for changes and we doesn't
97                         support any mege feature here. They are written only. The job has
98                         to modify this list.
99                     </li>
100                     <li>
101                         "SendDispatchResult" [css.frame.DispatchResultEvent]
102                         <br>
103                         The given event is send to all current registered listener.
104                         But it's not guaranteed. In case no listener are available or
105                         this job isn't part of the dispatch environment (because it was used
106                         by the css..task.XJobExecutor->trigger() implementation) this option
107                         will be ignored.
108                     </li>
109                     <li>
110                         "Deactivate" [boolean]
111                         <br>
112                         The job whish to be disabled. But note: There is no way, to enable it later
113                         again by using this implementation. It can be done by using the configuration
114                         only. (Means to register this job again.)
115                         If a job knows, that there exist some status or result listener, it must use
116                         the options "SendDispatchStatus" and "SendDispatchResult" (see before) too, to
117                         inform it about the deactivation of this service.
118                     </li>
119                 </ul>
120 
121     @param      aResult
122                     the job result
123 */
124 JobResult::JobResult( /*IN*/ const css::uno::Any& aResult )
125     : ThreadHelpBase(&Application::GetSolarMutex())
126 {
127     // safe the pure result
128     // May someone need it later ...
129     m_aPureResult = aResult;
130 
131     // reset the flag mask!
132     // It will reset the accessible state of this object.
133     // That can be usefull if something will fail here ...
134     m_eParts = E_NOPART;
135 
136     // analyze the result and update our other members
137     ::comphelper::SequenceAsHashMap aProtocol(aResult);
138     if ( aProtocol.empty() )
139         return;
140 
141     ::comphelper::SequenceAsHashMap::const_iterator pIt = aProtocol.end();
142 
143     pIt = aProtocol.find(JobConst::ANSWER_DEACTIVATE_JOB());
144     if (pIt != aProtocol.end())
145     {
146         pIt->second >>= m_bDeactivate;
147         if (m_bDeactivate)
148             m_eParts |= E_DEACTIVATE;
149     }
150 
151     pIt = aProtocol.find(JobConst::ANSWER_SAVE_ARGUMENTS());
152     if (pIt != aProtocol.end())
153     {
154         pIt->second >>= m_lArguments;
155         if (m_lArguments.getLength() > 0)
156             m_eParts |= E_ARGUMENTS;
157     }
158 
159     pIt = aProtocol.find(JobConst::ANSWER_SEND_DISPATCHRESULT());
160     if (pIt != aProtocol.end())
161     {
162         if (pIt->second >>= m_aDispatchResult)
163             m_eParts |= E_DISPATCHRESULT;
164     }
165 }
166 
167 //________________________________
168 /**
169     @short      copy dtor
170     @descr      -
171 */
172 JobResult::JobResult( const JobResult& rCopy )
173     : ThreadHelpBase(&Application::GetSolarMutex())
174 {
175     m_aPureResult     = rCopy.m_aPureResult     ;
176     m_eParts          = rCopy.m_eParts          ;
177     m_lArguments      = rCopy.m_lArguments      ;
178     m_bDeactivate     = rCopy.m_bDeactivate     ;
179     m_aDispatchResult = rCopy.m_aDispatchResult ;
180 }
181 
182 //________________________________
183 /**
184     @short      standard dtor
185     @descr      Free all internaly used ressources at the end of living.
186 */
187 JobResult::~JobResult()
188 {
189     // Nothing realy to do here.
190 }
191 
192 //________________________________
193 /**
194     @short      =operator
195     @descr      Must be implemented to overwrite this instance with another one.
196 
197     @param      rCopy
198                     reference to the other instance, which should be used for copying.
199 */
200 void JobResult::operator=( const JobResult& rCopy )
201 {
202     /* SAFE { */
203     WriteGuard aWriteLock(m_aLock);
204     m_aPureResult     = rCopy.m_aPureResult     ;
205     m_eParts          = rCopy.m_eParts          ;
206     m_lArguments      = rCopy.m_lArguments      ;
207     m_bDeactivate     = rCopy.m_bDeactivate     ;
208     m_aDispatchResult = rCopy.m_aDispatchResult ;
209     aWriteLock.unlock();
210     /* } SAFE */
211 }
212 
213 //________________________________
214 /**
215     @short      checks for existing parts of the analyzed result
216     @descr      The internal flag mask was set after analyzing of the pure result.
217                 An user of us can check here, if the required part was realy part
218                 of this result. Otherwhise it would use invalid informations ...
219                 by using our other members!
220 
221     @param      eParts
222                     a flag mask too, which will be compared with our internaly set one.
223 
224     @return     We return true only, if any set flag of the given mask match.
225 */
226 sal_Bool JobResult::existPart( sal_uInt32 eParts ) const
227 {
228     /* SAFE { */
229     ReadGuard aReadLock(m_aLock);
230     return ((m_eParts & eParts) == eParts);
231     /* } SAFE */
232 }
233 
234 //________________________________
235 /**
236     @short      provides access to our internal members
237     @descr      The return value will be valid only in case a call of
238                 existPart(E_...) before returned true!
239 
240     @return     It returns the state of the internal member
241                 without any checks!
242 */
243 css::uno::Sequence< css::beans::NamedValue > JobResult::getArguments() const
244 {
245     /* SAFE { */
246     ReadGuard aReadLock(m_aLock);
247     return m_lArguments;
248     /* } SAFE */
249 }
250 
251 //________________________________
252 
253 css::frame::DispatchResultEvent JobResult::getDispatchResult() const
254 {
255     /* SAFE { */
256     ReadGuard aReadLock(m_aLock);
257     return m_aDispatchResult;
258     /* } SAFE */
259 }
260 
261 } // namespace framework
262