Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #include <vcl/svapp.hxx>
11 : #include <vcl/graph.hxx>
12 : #include <vcl/cvtgrf.hxx>
13 : #include <vcl/graphicfilter.hxx>
14 : #include <svx/xoutbmp.hxx>
15 : #include <svx/extedit.hxx>
16 : #include <svx/graphichelper.hxx>
17 : #include <sfx2/viewfrm.hxx>
18 : #include <sfx2/bindings.hxx>
19 : #include <osl/file.hxx>
20 : #include <osl/thread.hxx>
21 : #include <osl/process.h>
22 : #include <osl/time.h>
23 : #include <svtools/filechangedchecker.hxx>
24 : #include <unotools/ucbstreamhelper.hxx>
25 : #include <comphelper/processfactory.hxx>
26 : #include <boost/bind.hpp>
27 :
28 : #include <com/sun/star/system/SystemShellExecute.hpp>
29 : #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
30 :
31 : using namespace css::uno;
32 : using namespace css::system;
33 :
34 0 : ExternalToolEdit::ExternalToolEdit()
35 0 : : m_pGraphicObject(NULL)
36 : {
37 0 : }
38 :
39 0 : ExternalToolEdit::~ExternalToolEdit()
40 : {
41 0 : }
42 :
43 0 : void ExternalToolEdit::HandleCloseEvent(ExternalToolEdit* pData)
44 : {
45 0 : Graphic newGraphic;
46 :
47 : //import the temp file image stream into the newGraphic
48 0 : SvStream* pStream = utl::UcbStreamHelper::CreateStream(pData->m_aFileName, STREAM_READ);
49 0 : if(pStream)
50 : {
51 0 : GraphicConverter::Import(*pStream, newGraphic);
52 :
53 : // Now update the Graphic in the shell by re-reading from the newGraphic
54 0 : pData->Update( newGraphic );
55 :
56 0 : delete(pStream);
57 0 : }
58 0 : }
59 :
60 0 : IMPL_LINK (ExternalToolEdit, StartListeningEvent, void*, pEvent)
61 : {
62 : //Start an event listener implemented via VCL timeout
63 0 : ExternalToolEdit* pData = ( ExternalToolEdit* )pEvent;
64 :
65 0 : new FileChangedChecker(pData->m_aFileName, ::boost::bind(&HandleCloseEvent, pData));
66 :
67 0 : return 0;
68 : }
69 :
70 0 : void ExternalToolEdit::threadWorker(void* pThreadData)
71 : {
72 0 : ExternalToolEdit* pData = (ExternalToolEdit*) pThreadData;
73 :
74 : // Make an asynchronous call to listen to the event of temporary image file
75 : // getting changed
76 0 : Application::PostUserEvent( LINK( NULL, ExternalToolEdit, StartListeningEvent ), pThreadData);
77 :
78 : Reference<XSystemShellExecute> xSystemShellExecute(
79 0 : SystemShellExecute::create( ::comphelper::getProcessComponentContext() ) );
80 0 : xSystemShellExecute->execute( pData->m_aFileName, OUString(), SystemShellExecuteFlags::URIS_ONLY );
81 0 : }
82 :
83 0 : void ExternalToolEdit::Edit( GraphicObject* pGraphicObject )
84 : {
85 : //Get the graphic from the GraphicObject
86 0 : m_pGraphicObject = pGraphicObject;
87 0 : const Graphic aGraphic = pGraphicObject->GetGraphic();
88 :
89 : //get the Preferred File Extension for this graphic
90 0 : OUString fExtension;
91 0 : GraphicHelper::GetPreferredExtension(fExtension, aGraphic);
92 :
93 : //Create the temp File
94 0 : OUString aTempFileBase;
95 0 : OUString aTempFileName;
96 :
97 : oslFileHandle pHandle;
98 0 : osl::FileBase::createTempFile(0, &pHandle, &aTempFileBase);
99 :
100 : // Move it to a file name with image extension properly set
101 0 : aTempFileName = aTempFileBase + OUString('.') + OUString(fExtension);
102 0 : osl::File::move(aTempFileBase, aTempFileName);
103 :
104 : //Write Graphic to the Temp File
105 0 : GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
106 0 : sal_uInt16 nFilter(rGraphicFilter.GetExportFormatNumberForShortName(fExtension));
107 :
108 0 : OUString aFilter(rGraphicFilter.GetExportFormatShortName(nFilter));
109 :
110 : // Write the Graphic to the file now
111 0 : XOutBitmap::WriteGraphic(aGraphic, aTempFileName, aFilter, XOUTBMP_USE_NATIVE_IF_POSSIBLE | XOUTBMP_DONT_EXPAND_FILENAME);
112 :
113 : // There is a possiblity that sPath extension might have been changed if the
114 : // provided extension is not writable
115 0 : m_aFileName = aTempFileName;
116 :
117 : //Create a thread
118 :
119 : // Create the data that is needed by the thread later
120 0 : osl_createThread(ExternalToolEdit::threadWorker, this);
121 0 : }
122 :
123 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|