summaryrefslogtreecommitdiff
path: root/svx/source/core/extedit.cxx
blob: d3b16b65a356325b50ba7165eb5c38573a8a54cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <svx/extedit.hxx>

#include <vcl/svapp.hxx>
#include <vcl/graph.hxx>
#include <vcl/cvtgrf.hxx>
#include <vcl/graphicfilter.hxx>
#include <svx/xoutbmp.hxx>
#include <svx/graphichelper.hxx>
#include <svx/svdpagv.hxx>
#include <svx/svdograf.hxx>
#include <svx/fmview.hxx>
#include <svtools/grfmgr.hxx>
#include <sfx2/viewfrm.hxx>
#include <sfx2/bindings.hxx>
#include <salhelper/thread.hxx>
#include <osl/file.hxx>
#include <osl/thread.hxx>
#include <osl/process.h>
#include <osl/time.h>
#include <svtools/filechangedchecker.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <comphelper/processfactory.hxx>

#include <memory>

#include <com/sun/star/system/SystemShellExecute.hpp>
#include <com/sun/star/system/SystemShellExecuteFlags.hpp>

using namespace css::uno;
using namespace css::system;

ExternalToolEdit::ExternalToolEdit()
{
}

ExternalToolEdit::~ExternalToolEdit()
{
}

void ExternalToolEdit::HandleCloseEvent(ExternalToolEdit* pData)
{
    Graphic newGraphic;

    //import the temp file image stream into the newGraphic
    std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(pData->m_aFileName, StreamMode::READ));
    if(pStream)
    {
        GraphicConverter::Import(*pStream, newGraphic);

        // Now update the Graphic in the shell by re-reading from the newGraphic
        pData->Update( newGraphic );
    }
}

void ExternalToolEdit::StartListeningEvent()
{
    //Start an event listener implemented via VCL timeout
    assert(!m_pChecker.get());
    m_pChecker.reset(new FileChangedChecker(
            m_aFileName, [this] () { return HandleCloseEvent(this); }));
}

// self-destructing thread to make shell execute async
class ExternalToolEditThread
    : public ::salhelper::Thread
{
private:
    OUString const m_aFileName;

    virtual void execute() override;

public:
    explicit ExternalToolEditThread(OUString const& rFileName)
        : ::salhelper::Thread("ExternalToolEdit")
        , m_aFileName(rFileName)
    {}
};

void ExternalToolEditThread::execute()
{
    try
    {
        Reference<XSystemShellExecute> const xSystemShellExecute(
            SystemShellExecute::create( ::comphelper::getProcessComponentContext()));
        xSystemShellExecute->execute(m_aFileName, OUString(),
                SystemShellExecuteFlags::URIS_ONLY);
    }
    catch (Exception const& e)
    {
        SAL_WARN("svx", "ExternalToolEditThread: exception: " << e.Message);
    }
}

void ExternalToolEdit::Edit(GraphicObject const*const pGraphicObject)
{
    //Get the graphic from the GraphicObject
    const Graphic& aGraphic = pGraphicObject->GetGraphic();

    //get the Preferred File Extension for this graphic
    OUString fExtension;
    GraphicHelper::GetPreferredExtension(fExtension, aGraphic);

    //Create the temp File
    OUString aTempFileBase;
    OUString aTempFileName;

    osl::FileBase::RC rc =
        osl::FileBase::createTempFile(nullptr, nullptr, &aTempFileBase);
    if (osl::FileBase::E_None != rc)
    {
        SAL_WARN("svx", "ExternalToolEdit::Edit: cannot create temp file");
        return;
    }

    // Move it to a file name with image extension properly set
    aTempFileName = aTempFileBase + "." + OUString(fExtension);
    // FIXME: this is pretty stupid, need a better osl temp file API
    rc = osl::File::move(aTempFileBase, aTempFileName);
    if (osl::FileBase::E_None != rc)
    {
        SAL_WARN("svx", "ExternalToolEdit::Edit: cannot move temp file");
        return;
    }

    //Write Graphic to the Temp File
    GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
    sal_uInt16 nFilter(rGraphicFilter.GetExportFormatNumberForShortName(fExtension));

    OUString aFilter(rGraphicFilter.GetExportFormatShortName(nFilter));

    // Write the Graphic to the file now
    XOutBitmap::WriteGraphic(aGraphic, aTempFileName, aFilter, XOUTBMP_USE_NATIVE_IF_POSSIBLE | XOUTBMP_DONT_EXPAND_FILENAME);

    // There is a possibility that sPath extension might have been changed if the
    // provided extension is not writable
    m_aFileName = aTempFileName;

    //Create a thread

    rtl::Reference<ExternalToolEditThread> const pThread(
            new ExternalToolEditThread(m_aFileName));
    pThread->launch();

    StartListeningEvent();
}

SdrExternalToolEdit::SdrExternalToolEdit(
        FmFormView *const pView, SdrObject *const pObj)
    : m_pView(pView)
    , m_pObj(pObj)
{
    assert(m_pObj && m_pView);
    StartListening(*m_pObj->GetModel());
}


void SdrExternalToolEdit::Notify(SfxBroadcaster & rBC, SfxHint const& rHint)
{
    SdrHint const*const pSdrHint(dynamic_cast<SdrHint const*>(&rHint));
    if (pSdrHint
        && (HINT_MODELCLEARED == pSdrHint->GetKind()
            || (pSdrHint->GetObject() == m_pObj
                && HINT_OBJREMOVED == pSdrHint->GetKind())))
    {
        m_pView = nullptr;
        m_pObj = nullptr;
        m_pChecker.reset(); // avoid modifying deleted object
        EndListening(rBC);
    }
}

void SdrExternalToolEdit::Update(Graphic & rGraphic)
{
    assert(m_pObj && m_pView); // timer should be deleted by Notify() too
    SdrPageView *const pPageView = m_pView->GetSdrPageView();
    if (pPageView)
    {
        SdrGrafObj *const pNewObj(static_cast<SdrGrafObj*>(m_pObj->Clone()));
        assert(pNewObj);
        OUString const description =
            m_pView->GetDescriptionOfMarkedObjects() + " External Edit";
        m_pView->BegUndo(description);
        pNewObj->SetGraphicObject(rGraphic);
        // set to new object before ReplaceObjectAtView() so that Notify() will
        // not delete the running timer and crash
        SdrObject *const pOldObj = m_pObj;
        m_pObj = pNewObj;
        m_pView->ReplaceObjectAtView(pOldObj, *pPageView, pNewObj);
        m_pView->EndUndo();
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */