1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir  ************************************************************************/
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
28*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "onlogrotate_job.hxx"
31*cdf0e10cSrcweir #include "config.hxx"
32*cdf0e10cSrcweir #include "logpacker.hxx"
33*cdf0e10cSrcweir #include "logstorage.hxx"
34*cdf0e10cSrcweir #include "soaprequest.hxx"
35*cdf0e10cSrcweir #include "soapsender.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/frame/XTerminateListener.hpp>
40*cdf0e10cSrcweir #include <osl/conditn.hxx>
41*cdf0e10cSrcweir #include <osl/thread.hxx>
42*cdf0e10cSrcweir #include <osl/time.h>
43*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
44*cdf0e10cSrcweir #include <memory>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
48*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
49*cdf0e10cSrcweir using namespace ::com::sun::star::task;
50*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
51*cdf0e10cSrcweir using ::com::sun::star::frame::XTerminateListener;
52*cdf0e10cSrcweir using ::com::sun::star::frame::XDesktop;
53*cdf0e10cSrcweir using ::com::sun::star::ucb::XSimpleFileAccess;
54*cdf0e10cSrcweir using ::rtl::OUString;
55*cdf0e10cSrcweir using ::std::vector;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir namespace
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     using namespace oooimprovement;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     static void packLogs(const Reference<XMultiServiceFactory>& sf)
62*cdf0e10cSrcweir     {
63*cdf0e10cSrcweir         try
64*cdf0e10cSrcweir         {
65*cdf0e10cSrcweir             Config config(sf);
66*cdf0e10cSrcweir             LogPacker log_packer(sf);
67*cdf0e10cSrcweir             vector<OUString> csvfiles = LogStorage(sf).getUnzippedLogFiles();
68*cdf0e10cSrcweir             for(
69*cdf0e10cSrcweir                 vector<OUString>::iterator item = csvfiles.begin();
70*cdf0e10cSrcweir                 item!=csvfiles.end();
71*cdf0e10cSrcweir                 item++)
72*cdf0e10cSrcweir                 config.incrementEventCount(log_packer.pack(*item));
73*cdf0e10cSrcweir         } catch(...) {};
74*cdf0e10cSrcweir     };
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     static void uploadLogs(const Reference<XMultiServiceFactory>& sf)
77*cdf0e10cSrcweir     {
78*cdf0e10cSrcweir         try
79*cdf0e10cSrcweir         {
80*cdf0e10cSrcweir             Config config(sf);
81*cdf0e10cSrcweir             Reference<XSimpleFileAccess> file_access(
82*cdf0e10cSrcweir                 sf->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
83*cdf0e10cSrcweir                 UNO_QUERY_THROW);
84*cdf0e10cSrcweir             SoapSender sender(sf, config.getSoapUrl());
85*cdf0e10cSrcweir             OUString soap_id = config.getSoapId();
86*cdf0e10cSrcweir             vector<OUString> zipfiles = LogStorage(sf).getZippedLogFiles();
87*cdf0e10cSrcweir             for(
88*cdf0e10cSrcweir                 vector<OUString>::iterator item = zipfiles.begin();
89*cdf0e10cSrcweir                 item!=zipfiles.end();
90*cdf0e10cSrcweir                 item++)
91*cdf0e10cSrcweir             {
92*cdf0e10cSrcweir                 if(config.incrementFailedAttempts(1) > 25)
93*cdf0e10cSrcweir                 {
94*cdf0e10cSrcweir                     config.giveupUploading();
95*cdf0e10cSrcweir                     LogStorage(sf).clear();
96*cdf0e10cSrcweir                     return;
97*cdf0e10cSrcweir                 }
98*cdf0e10cSrcweir                 sender.send(SoapRequest(sf, soap_id, *item));
99*cdf0e10cSrcweir                 config.incrementReportCount(1);
100*cdf0e10cSrcweir                 file_access->kill(*item);
101*cdf0e10cSrcweir                 config.resetFailedAttempts();
102*cdf0e10cSrcweir             }
103*cdf0e10cSrcweir         } catch(...) {};
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     class OnLogRotateThread : public ::osl::Thread
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         public:
109*cdf0e10cSrcweir             OnLogRotateThread(Reference<XMultiServiceFactory> sf);
110*cdf0e10cSrcweir             virtual void SAL_CALL run();
111*cdf0e10cSrcweir             void stop();
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         private:
114*cdf0e10cSrcweir             Reference<XMultiServiceFactory> m_ServiceFactory;
115*cdf0e10cSrcweir             ::osl::Condition m_Stop;
116*cdf0e10cSrcweir     };
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     OnLogRotateThread::OnLogRotateThread(Reference<XMultiServiceFactory> sf)
119*cdf0e10cSrcweir         : m_ServiceFactory(sf)
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         OSL_ASSERT(sf.is());
122*cdf0e10cSrcweir     }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     void SAL_CALL OnLogRotateThread::run()
125*cdf0e10cSrcweir     {
126*cdf0e10cSrcweir         TimeValue wait_intervall = {30,0};
127*cdf0e10cSrcweir         if (m_Stop.wait(&wait_intervall) == ::osl::Condition::result_timeout)
128*cdf0e10cSrcweir         {
129*cdf0e10cSrcweir             try
130*cdf0e10cSrcweir             {
131*cdf0e10cSrcweir                 if(Config(m_ServiceFactory).getInvitationAccepted())
132*cdf0e10cSrcweir                 {
133*cdf0e10cSrcweir                     packLogs(m_ServiceFactory);
134*cdf0e10cSrcweir                     uploadLogs(m_ServiceFactory);
135*cdf0e10cSrcweir                 }
136*cdf0e10cSrcweir                 else
137*cdf0e10cSrcweir                     LogStorage(m_ServiceFactory).clear();
138*cdf0e10cSrcweir             }
139*cdf0e10cSrcweir             catch(...) {}
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     void OnLogRotateThread::stop()
144*cdf0e10cSrcweir     {
145*cdf0e10cSrcweir         m_Stop.set();
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     class OnLogRotateThreadWatcher : public ::cppu::WeakImplHelper1<XTerminateListener>
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir         public:
151*cdf0e10cSrcweir             OnLogRotateThreadWatcher(Reference<XMultiServiceFactory> sf)
152*cdf0e10cSrcweir                 : m_Thread(new OnLogRotateThread(sf))
153*cdf0e10cSrcweir             {
154*cdf0e10cSrcweir                 m_Thread->create();
155*cdf0e10cSrcweir             }
156*cdf0e10cSrcweir             virtual ~OnLogRotateThreadWatcher()
157*cdf0e10cSrcweir             {
158*cdf0e10cSrcweir                 m_Thread->stop();
159*cdf0e10cSrcweir                 m_Thread->join();
160*cdf0e10cSrcweir             };
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir             // XTerminateListener
163*cdf0e10cSrcweir             virtual void SAL_CALL queryTermination(const EventObject&) throw(RuntimeException)
164*cdf0e10cSrcweir                 { };
165*cdf0e10cSrcweir             virtual void SAL_CALL notifyTermination(const EventObject&) throw(RuntimeException)
166*cdf0e10cSrcweir             {
167*cdf0e10cSrcweir                 m_Thread->stop();
168*cdf0e10cSrcweir                 m_Thread->join();
169*cdf0e10cSrcweir             };
170*cdf0e10cSrcweir             // XEventListener
171*cdf0e10cSrcweir             virtual void SAL_CALL disposing(const EventObject&) throw(RuntimeException)
172*cdf0e10cSrcweir             {
173*cdf0e10cSrcweir                 m_Thread->stop();
174*cdf0e10cSrcweir                 m_Thread->join();
175*cdf0e10cSrcweir             };
176*cdf0e10cSrcweir         private:
177*cdf0e10cSrcweir             ::std::auto_ptr<OnLogRotateThread> m_Thread;
178*cdf0e10cSrcweir     };
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir namespace oooimprovement
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     OnLogRotateJob::OnLogRotateJob(const Reference<XComponentContext>& context)
184*cdf0e10cSrcweir         : m_ServiceFactory(Reference<XMultiServiceFactory>(
185*cdf0e10cSrcweir             context->getServiceManager()->createInstanceWithContext(
186*cdf0e10cSrcweir                 OUString::createFromAscii("com.sun.star.lang.XMultiServiceFactory"), context),
187*cdf0e10cSrcweir             UNO_QUERY))
188*cdf0e10cSrcweir     { }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     OnLogRotateJob::OnLogRotateJob(const Reference<XMultiServiceFactory>& sf)
191*cdf0e10cSrcweir         : m_ServiceFactory(sf)
192*cdf0e10cSrcweir     { }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     OnLogRotateJob::~OnLogRotateJob()
195*cdf0e10cSrcweir     { }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     void SAL_CALL OnLogRotateJob::executeAsync(
198*cdf0e10cSrcweir         const Sequence<NamedValue>&,
199*cdf0e10cSrcweir         const Reference<XJobListener>& listener)
200*cdf0e10cSrcweir         throw(RuntimeException)
201*cdf0e10cSrcweir     {
202*cdf0e10cSrcweir         Reference<XDesktop> xDesktop(
203*cdf0e10cSrcweir             m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.frame.Desktop")),
204*cdf0e10cSrcweir             UNO_QUERY);
205*cdf0e10cSrcweir         if(xDesktop.is())
206*cdf0e10cSrcweir             xDesktop->addTerminateListener(Reference<XTerminateListener>(new OnLogRotateThreadWatcher(m_ServiceFactory)));
207*cdf0e10cSrcweir         Any result;
208*cdf0e10cSrcweir         listener->jobFinished(Reference<XAsyncJob>(this), result);
209*cdf0e10cSrcweir     }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir     sal_Bool SAL_CALL OnLogRotateJob::supportsService(const OUString& service_name) throw(RuntimeException)
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         const Sequence<OUString> service_names(getSupportedServiceNames());
214*cdf0e10cSrcweir         for (sal_Int32 idx = service_names.getLength()-1; idx>=0; --idx)
215*cdf0e10cSrcweir             if(service_name == service_names[idx]) return sal_True;
216*cdf0e10cSrcweir         return sal_False;
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir     OUString SAL_CALL OnLogRotateJob::getImplementationName() throw(RuntimeException)
220*cdf0e10cSrcweir     { return getImplementationName_static(); }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     Sequence<OUString> SAL_CALL OnLogRotateJob::getSupportedServiceNames() throw(RuntimeException)
223*cdf0e10cSrcweir     { return getSupportedServiceNames_static(); }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     OUString SAL_CALL OnLogRotateJob::getImplementationName_static()
226*cdf0e10cSrcweir     { return OUString::createFromAscii("com.sun.star.comp.extensions.oooimprovement.OnLogRotateJob"); }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir     Sequence<OUString> SAL_CALL OnLogRotateJob::getSupportedServiceNames_static()
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir         Sequence<OUString> aServiceNames(1);
231*cdf0e10cSrcweir         aServiceNames[0] = OUString::createFromAscii("com.sun.star.task.AsyncJob");
232*cdf0e10cSrcweir         return aServiceNames;
233*cdf0e10cSrcweir     }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir     Reference<XInterface> OnLogRotateJob::Create(const Reference<XComponentContext>& context)
236*cdf0e10cSrcweir     { return *(new OnLogRotateJob(context)); }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir     Reference<XInterface> OnLogRotateJob::Create(const Reference<XMultiServiceFactory>& sf)
239*cdf0e10cSrcweir     { return *(new OnLogRotateJob(sf)); }
240*cdf0e10cSrcweir }
241