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 : {}
36 :
37 0 : ExternalToolEdit::~ExternalToolEdit()
38 0 : {}
39 :
40 0 : void ExternalToolEdit::HandleCloseEvent(ExternalToolEdit* pData)
41 : {
42 0 : Graphic newGraphic;
43 :
44 : //import the temp file image stream into the newGraphic
45 0 : SvStream* pStream = utl::UcbStreamHelper::CreateStream(pData->m_aFileName, STREAM_READ);
46 0 : if(pStream)
47 : {
48 0 : GraphicConverter::Import(*pStream, newGraphic);
49 :
50 : // Now update the Graphic in the shell by re-reading from the newGraphic
51 0 : pData->Update( newGraphic );
52 :
53 0 : delete(pStream);
54 0 : }
55 0 : }
56 :
57 0 : IMPL_LINK (ExternalToolEdit, StartListeningEvent, void*, pEvent)
58 : {
59 : //Start an event listener implemented via VCL timeout
60 0 : ExternalToolEdit* pData = ( ExternalToolEdit* )pEvent;
61 :
62 0 : new FileChangedChecker(pData->m_aFileName, ::boost::bind(&HandleCloseEvent, pData));
63 :
64 0 : return 0;
65 : }
66 :
67 0 : void ExternalToolEdit::threadWorker(void* pThreadData)
68 : {
69 0 : ExternalToolEdit* pData = (ExternalToolEdit*) pThreadData;
70 :
71 : // Make an asynchronous call to listen to the event of temporary image file
72 : // getting changed
73 0 : Application::PostUserEvent( LINK( NULL, ExternalToolEdit, StartListeningEvent ), pThreadData);
74 :
75 : Reference<XSystemShellExecute> xSystemShellExecute(
76 0 : SystemShellExecute::create( ::comphelper::getProcessComponentContext() ) );
77 0 : xSystemShellExecute->execute( pData->m_aFileName, OUString(), SystemShellExecuteFlags::URIS_ONLY );
78 0 : }
79 :
80 0 : void ExternalToolEdit::Edit( GraphicObject* pGraphicObject )
81 : {
82 : //Get the graphic from the GraphicObject
83 0 : m_pGraphicObject = pGraphicObject;
84 0 : const Graphic aGraphic = pGraphicObject->GetGraphic();
85 :
86 : //get the Preferred File Extension for this graphic
87 0 : OUString fExtension;
88 0 : GraphicHelper::GetPreferedExtension(fExtension, aGraphic);
89 :
90 : //Create the temp File
91 0 : OUString aTempFileBase;
92 0 : OUString aTempFileName;
93 :
94 : oslFileHandle pHandle;
95 0 : osl::FileBase::createTempFile(0, &pHandle, &aTempFileBase);
96 :
97 : // Move it to a file name with image extension properly set
98 0 : aTempFileName = aTempFileBase + OUString('.') + OUString(fExtension);
99 0 : osl::File::move(aTempFileBase, aTempFileName);
100 :
101 : //Write Graphic to the Temp File
102 0 : GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
103 0 : sal_uInt16 nFilter(rGraphicFilter.GetExportFormatNumber(fExtension));
104 :
105 0 : String aFilter(rGraphicFilter.GetExportFormatShortName(nFilter));
106 0 : String sPath(aTempFileName);
107 :
108 : // Write the Graphic to the file now
109 0 : XOutBitmap::WriteGraphic(aGraphic, sPath, aFilter, XOUTBMP_USE_NATIVE_IF_POSSIBLE | XOUTBMP_DONT_EXPAND_FILENAME);
110 :
111 : // There is a possiblity that sPath extnesion might have been changed if the
112 : // provided extension is not writable
113 0 : m_aFileName = OUString(sPath);
114 :
115 : //Create a thread
116 :
117 : // Create the data that is needed by the thread later
118 0 : osl_createThread(ExternalToolEdit::threadWorker, this);
119 258 : }
|