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