Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * [ Surendran Mahendran <surenspost@gmail.com>]
17 : *
18 : * Alternatively, the contents of this file may be used under the terms of
19 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
20 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
21 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
22 : * instead of those above.
23 : */
24 :
25 : #include <svx/extedit.hxx>
26 : #include <svx/graphichelper.hxx>
27 : #include <sfx2/viewfrm.hxx>
28 : #include <sfx2/bindings.hxx>
29 : #include <osl/file.hxx>
30 : #include <osl/thread.hxx>
31 : #include <osl/process.h>
32 : #include <osl/time.h>
33 : #include <svtools/filter.hxx>
34 : #include <svtools/filechangedchecker.hxx>
35 : #include <svx/xoutbmp.hxx>
36 : #include <unotools/ucbstreamhelper.hxx>
37 : #include <vcl/svapp.hxx>
38 : #include <vcl/graph.hxx>
39 : #include <vcl/cvtgrf.hxx>
40 :
41 : #include "com/sun/star/system/SystemShellExecute.hpp"
42 : #include "com/sun/star/system/SystemShellExecuteFlags.hpp"
43 : #include <comphelper/processfactory.hxx>
44 :
45 : #include <boost/bind.hpp>
46 :
47 : using namespace ::com::sun::star;
48 0 : ExternalToolEdit::ExternalToolEdit()
49 0 : {}
50 :
51 0 : ExternalToolEdit::~ExternalToolEdit()
52 0 : {}
53 :
54 0 : void ExternalToolEdit::HandleCloseEvent(ExternalToolEdit* pData)
55 : {
56 0 : Graphic newGraphic;
57 :
58 : //import the temp file image stream into the newGraphic
59 0 : SvStream* pStream = utl::UcbStreamHelper::CreateStream(pData->m_aFileName, STREAM_READ);
60 0 : if(pStream)
61 : {
62 0 : GraphicConverter::Import(*pStream, newGraphic);
63 :
64 : // Now update the Graphic in the shell by re-reading from the newGraphic
65 0 : pData->Update( newGraphic );
66 :
67 0 : delete(pStream);
68 0 : }
69 0 : }
70 :
71 0 : IMPL_LINK (ExternalToolEdit, StartListeningEvent, void*, pEvent)
72 : {
73 : //Start an event listener implemented via VCL timeout
74 0 : ExternalToolEdit* pData = ( ExternalToolEdit* )pEvent;
75 0 : String aURL( pData->m_aFileName );
76 :
77 : new FileChangedChecker(
78 : pData->m_aFileName,
79 0 : ::boost::bind(&HandleCloseEvent, pData));
80 :
81 0 : return 0;
82 : }
83 :
84 0 : void ExternalToolEdit::threadWorker(void* pThreadData)
85 : {
86 0 : ExternalToolEdit* pData = (ExternalToolEdit*) pThreadData;
87 :
88 : // Make an asynchronous call to listen to the event of temporary image file
89 : // getting changed
90 0 : Application::PostUserEvent( LINK( NULL, ExternalToolEdit, StartListeningEvent ), pThreadData);
91 :
92 : uno::Reference< com::sun::star::system::XSystemShellExecute > xSystemShellExecute(
93 0 : com::sun::star::system::SystemShellExecute::create(::comphelper::getProcessComponentContext() ) );
94 0 : xSystemShellExecute->execute( pData->m_aFileName, rtl::OUString(), com::sun::star::system::SystemShellExecuteFlags::URIS_ONLY );
95 0 : }
96 :
97 0 : void ExternalToolEdit::Edit( GraphicObject* pGraphicObject )
98 : {
99 : //Get the graphic from the GraphicObject
100 0 : m_pGraphicObject = pGraphicObject;
101 0 : const Graphic aGraphic = pGraphicObject->GetGraphic();
102 :
103 : //get the Preferred File Extension for this graphic
104 0 : String fExtension;
105 0 : GraphicHelper::GetPreferedExtension(fExtension, aGraphic);
106 :
107 : //Create the temp File
108 0 : rtl::OUString tempFileBase, tempFileName;
109 : oslFileHandle pHandle;
110 0 : osl::FileBase::createTempFile(0, &pHandle, &tempFileBase);
111 :
112 : // Move it to a file name with image extension properly set
113 0 : tempFileName = tempFileBase + rtl::OUString('.') + rtl::OUString(fExtension);
114 0 : osl::File::move(tempFileBase, tempFileName);
115 :
116 : //Write Graphic to the Temp File
117 0 : GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
118 0 : sal_uInt16 nFilter(rGraphicFilter.GetExportFormatNumber(fExtension));
119 0 : String aFilter(rGraphicFilter.GetExportFormatShortName(nFilter));
120 0 : String sPath(tempFileName);
121 :
122 : // Write the Graphic to the file now
123 0 : XOutBitmap::WriteGraphic(aGraphic, sPath, aFilter, XOUTBMP_USE_NATIVE_IF_POSSIBLE|XOUTBMP_DONT_EXPAND_FILENAME);
124 :
125 : // There is a possiblity that sPath extnesion might have been changed if the
126 : // provided extension is not writable
127 0 : tempFileName = rtl::OUString(sPath);
128 :
129 : //Create a thread
130 0 : rtl_uString* aFileName = new rtl_uString();
131 : rtl_uString_newFromAscii(
132 : &aFileName,
133 0 : rtl::OUStringToOString(tempFileName, RTL_TEXTENCODING_UTF8).getStr());
134 0 : m_aFileName = aFileName;
135 : // Create the data that is needed by the thread later
136 0 : osl_createThread(ExternalToolEdit::threadWorker, this);
137 63 : }
|