summaryrefslogtreecommitdiff
path: root/cui/source/dialogs/hldocntp.cxx
blob: 3e875bef1994591bea179be86cb4606b30aceaec (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <hldocntp.hxx>
#include <osl/file.hxx>
#include <sfx2/viewfrm.hxx>
#include <sfx2/docfilt.hxx>
#include <svl/stritem.hxx>
#include <com/sun/star/awt/XTopWindow.hpp>
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/uno/Sequence.h>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/uno/Exception.hpp>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <tools/urlobj.hxx>
#include <unotools/pathoptions.hxx>
#include <unotools/dynamicmenuoptions.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <unotools/ucbhelper.hxx>

#include <comphelper/processfactory.hxx>
#include <com/sun/star/ui/dialogs/FolderPicker.hpp>
#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>

#include <cuihyperdlg.hxx>
#include <dialmgr.hxx>
#include <strings.hrc>

using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::ui::dialogs;
using namespace ::com::sun::star::uno;

using namespace ::com::sun::star;

/*************************************************************************
|*
|* Data-struct for documenttypes in listbox
|*
|************************************************************************/

struct DocumentTypeData
{
    OUString aStrURL;
    OUString aStrExt;
    DocumentTypeData (const OUString& aURL, const OUString& aExt) : aStrURL(aURL), aStrExt(aExt)
    {}
};

bool SvxHyperlinkNewDocTp::ImplGetURLObject( const OUString& rPath, const OUString& rBase, INetURLObject& aURLObject ) const
{
    bool bIsValidURL = !rPath.isEmpty();
    if ( bIsValidURL )
    {
        aURLObject.SetURL( rPath );
        if ( aURLObject.GetProtocol() == INetProtocol::NotValid )      // test if the source is already a valid url
        {                                                           // if not we have to create a url from a physical file name
            bool wasAbs;
            INetURLObject base(rBase);
            base.setFinalSlash();
            aURLObject = base.smartRel2Abs(
                rPath, wasAbs, true, INetURLObject::EncodeMechanism::All,
                RTL_TEXTENCODING_UTF8, true);
        }
        bIsValidURL = aURLObject.GetProtocol() != INetProtocol::NotValid;
        if ( bIsValidURL )
        {
            OUString aBase( aURLObject.getName( INetURLObject::LAST_SEGMENT, false ) );
            if ( aBase.isEmpty() || ( aBase[0] == '.' ) )
                bIsValidURL = false;
        }
        if ( bIsValidURL )
        {
            sal_Int32 nPos = m_xLbDocTypes->get_selected_index();
            if (nPos != -1)
                aURLObject.SetExtension(reinterpret_cast<DocumentTypeData*>(m_xLbDocTypes->get_id(nPos).toInt64())->aStrExt);
        }

    }
    return bIsValidURL;
}

/*************************************************************************
|*
|* Constructor / Destructor
|*
|************************************************************************/

SvxHyperlinkNewDocTp::SvxHyperlinkNewDocTp(weld::Container* pParent, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
    : SvxHyperlinkTabPageBase(pParent, pDlg, "cui/ui/hyperlinknewdocpage.ui", "HyperlinkNewDocPage", pItemSet)
    , m_xRbtEditNow(xBuilder->weld_radio_button("editnow"))
    , m_xRbtEditLater(xBuilder->weld_radio_button("editlater"))
    , m_xCbbPath(new SvxHyperURLBox(xBuilder->weld_combo_box("path")))
    , m_xBtCreate(xBuilder->weld_button("create"))
    , m_xLbDocTypes(xBuilder->weld_tree_view("types"))
{
    m_xCbbPath->SetSmartProtocol(INetProtocol::File);
    m_xLbDocTypes->set_size_request(-1, m_xLbDocTypes->get_height_rows(5));

    InitStdControls();

    SetExchangeSupport ();

    m_xCbbPath->show();
    m_xCbbPath->SetBaseURL(SvtPathOptions().GetWorkPath());

    // set defaults
    m_xRbtEditNow->set_active(true);

    m_xBtCreate->connect_clicked(LINK(this, SvxHyperlinkNewDocTp, ClickNewHdl_Impl));

    FillDocumentList ();
}

SvxHyperlinkNewDocTp::~SvxHyperlinkNewDocTp ()
{
    if (m_xLbDocTypes)
    {
        for (sal_Int32 n = 0, nEntryCount = m_xLbDocTypes->n_children(); n < nEntryCount; ++n)
            delete reinterpret_cast<DocumentTypeData*>(m_xLbDocTypes->get_id(n).toInt64());
        m_xLbDocTypes = nullptr;
    }
}

/*************************************************************************
|*
|* Fill the all dialog-controls except controls in groupbox "more..."
|*
|************************************************************************/


void SvxHyperlinkNewDocTp::FillDlgFields(const OUString& /*rStrURL*/)
{
}

void SvxHyperlinkNewDocTp::FillDocumentList()
{
    weld::WaitObject aWaitObj(mpDialog->getDialog());

    uno::Sequence< uno::Sequence< beans::PropertyValue > >
        aDynamicMenuEntries( SvtDynamicMenuOptions().GetMenu( EDynamicMenuType::NewMenu ) );

    sal_uInt32 i, nCount = aDynamicMenuEntries.getLength();
    for ( i = 0; i < nCount; i++ )
    {
        uno::Sequence< beans::PropertyValue >& rDynamicMenuEntry = aDynamicMenuEntries[ i ];

        OUString aDocumentUrl, aTitle;

        for ( int e = 0; e < rDynamicMenuEntry.getLength(); e++ )
        {
            if ( rDynamicMenuEntry[ e ].Name == DYNAMICMENU_PROPERTYNAME_URL )
                rDynamicMenuEntry[ e ].Value >>= aDocumentUrl;
            else if ( rDynamicMenuEntry[e].Name == DYNAMICMENU_PROPERTYNAME_TITLE )
                rDynamicMenuEntry[e].Value >>= aTitle;
        }
        //#i96822# business cards, labels and database should not be inserted here
        if( aDocumentUrl == "private:factory/swriter?slot=21051" ||
            aDocumentUrl == "private:factory/swriter?slot=21052" ||
            aDocumentUrl == "private:factory/sdatabase?Interactive" )
            continue;

        // Insert into listbox
        if ( !aDocumentUrl.isEmpty() )
        {
            if ( aDocumentUrl == "private:factory/simpress?slot=6686" )              // SJ: #106216# do not start
                aDocumentUrl = "private:factory/simpress"; // the AutoPilot for impress

            // insert private-url and default-extension as user-data
            std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetDefaultFilterFromFactory( aDocumentUrl );
            if ( pFilter )
            {
                // insert doc-name and image
                OUString aTitleName = aTitle.replaceFirst( "~", "" );

                OUString aStrDefExt(pFilter->GetDefaultExtension());
                DocumentTypeData *pTypeData = new DocumentTypeData(aDocumentUrl, aStrDefExt.copy(2));
                OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pTypeData)));
                m_xLbDocTypes->append(sId, aTitleName);
            }
        }
    }
    m_xLbDocTypes->select(0);
}

/*************************************************************************
|*
|* retrieve and prepare data from dialog-fields
|*
|************************************************************************/

void SvxHyperlinkNewDocTp::GetCurentItemData ( OUString& rStrURL, OUString& aStrName,
                                               OUString& aStrIntName, OUString& aStrFrame,
                                               SvxLinkInsertMode& eMode )
{
    // get data from dialog-controls
    rStrURL = m_xCbbPath->get_active_text();
    INetURLObject aURL;
    if ( ImplGetURLObject( rStrURL, m_xCbbPath->GetBaseURL(), aURL ) )
    {
        rStrURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
    }

    GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
}

/*************************************************************************
|*
|* static method to create Tabpage
|*
|************************************************************************/

std::unique_ptr<IconChoicePage> SvxHyperlinkNewDocTp::Create(weld::Container* pWindow, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
{
    return std::make_unique<SvxHyperlinkNewDocTp>(pWindow, pDlg, pItemSet);
}

/*************************************************************************
|*
|* Set initial focus
|*
|************************************************************************/
void SvxHyperlinkNewDocTp::SetInitFocus()
{
    m_xCbbPath->grab_focus();
}

/*************************************************************************
|*
|* Ask page whether an insert is possible
|*
\************************************************************************/
bool SvxHyperlinkNewDocTp::AskApply()
{
    INetURLObject aINetURLObject;
    bool bRet = ImplGetURLObject(m_xCbbPath->get_active_text(), m_xCbbPath->GetBaseURL(), aINetURLObject);
    if ( !bRet )
    {
        std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(mpDialog->getDialog(),
                                                   VclMessageType::Warning, VclButtonsType::Ok,
                                                   CuiResId(RID_SVXSTR_HYPDLG_NOVALIDFILENAME)));
        xWarn->run();
    }
    return bRet;
}

namespace
{
    struct ExecuteInfo
    {
        bool bRbtEditLater;
        bool bRbtEditNow;
        INetURLObject aURL;
        OUString aStrDocName;
        // current document
        css::uno::Reference<css::frame::XFrame> xFrame;
        SfxDispatcher* pDispatcher;
    };
}

IMPL_STATIC_LINK(SvxHyperlinkNewDocTp, DispatchDocument, void*, p, void)
{
    std::unique_ptr<ExecuteInfo> xExecuteInfo(static_cast<ExecuteInfo*>(p));
    if (!xExecuteInfo->xFrame.is())
        return;
    try
    {
        //if it throws dispatcher is invalid
        css::uno::Reference<css::awt::XTopWindow>(xExecuteInfo->xFrame->getContainerWindow(), css::uno::UNO_QUERY_THROW);

        SfxViewFrame *pViewFrame = nullptr;

        // create items
        SfxStringItem aName( SID_FILE_NAME, xExecuteInfo->aStrDocName );
        SfxStringItem aReferer( SID_REFERER, "private:user" );
        SfxStringItem aFrame( SID_TARGETNAME, "_blank");

        OUString aStrFlags('S');
        if (xExecuteInfo->bRbtEditLater)
        {
            aStrFlags += "H";
        }
        SfxStringItem aFlags (SID_OPTIONS, aStrFlags);

        // open url
        const SfxPoolItem* pReturn = xExecuteInfo->pDispatcher->ExecuteList(
                SID_OPENDOC, SfxCallMode::SYNCHRON,
                { &aName, &aFlags, &aFrame, &aReferer });

        // save new doc
        const SfxViewFrameItem *pItem = dynamic_cast<const SfxViewFrameItem*>( pReturn  );  // SJ: pReturn is NULL if the Hyperlink
        if ( pItem )                                                            // creation is cancelled #106216#
        {
            pViewFrame = pItem->GetFrame();
            if (pViewFrame)
            {
                SfxStringItem aNewName( SID_FILE_NAME, xExecuteInfo->aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
                SfxUnoFrameItem aDocFrame( SID_FILLFRAME, pViewFrame->GetFrame().GetFrameInterface() );
                fprintf(stderr, "is there a frame int %p\n", pViewFrame->GetFrame().GetFrameInterface().get() );
                pViewFrame->GetDispatcher()->ExecuteList(
                    SID_SAVEASDOC, SfxCallMode::SYNCHRON,
                    { &aNewName }, { &aDocFrame });
            }
        }

        if (xExecuteInfo->bRbtEditNow)
        {
            css::uno::Reference<css::awt::XTopWindow> xWindow(xExecuteInfo->xFrame->getContainerWindow(), css::uno::UNO_QUERY);
            if (xWindow.is()) //will be false if the frame was exited while the document was loading (e.g. we waited for warning dialogs)
                xWindow->toFront();
        }

        if (pViewFrame && xExecuteInfo->bRbtEditLater)
        {
            SfxObjectShell* pObjShell = pViewFrame->GetObjectShell();
            pObjShell->DoClose();
        }
    }
    catch (...)
    {
    }
}

/*************************************************************************
|*
|* Any action to do after apply-button is pressed
|*
\************************************************************************/
void SvxHyperlinkNewDocTp::DoApply()
{
    weld::WaitObject aWait(mpDialog->getDialog());

    // get data from dialog-controls
    OUString aStrNewName = m_xCbbPath->get_active_text();

    if ( aStrNewName.isEmpty() )
        aStrNewName = maStrInitURL;

    // create a real URL-String
    INetURLObject aURL;
    if ( ImplGetURLObject( aStrNewName, m_xCbbPath->GetBaseURL(), aURL ) )
    {
        // create Document
        aStrNewName = aURL.GetURLPath( INetURLObject::DecodeMechanism::NONE );
        bool bCreate = true;
        try
        {
            // check if file exists, warn before we overwrite it
            std::unique_ptr<SvStream> pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ );

            bool bOk = pIStm && ( pIStm->GetError() == ERRCODE_NONE);

            pIStm.reset();

            if( bOk )
            {
                std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(mpDialog->getDialog(),
                                                           VclMessageType::Warning, VclButtonsType::YesNo,
                                                           CuiResId(RID_SVXSTR_HYPERDLG_QUERYOVERWRITE)));
                bCreate = xWarn->run() == RET_YES;
            }
        }
        catch (const uno::Exception&)
        {
        }

        if (bCreate && !aStrNewName.isEmpty())
        {
            ExecuteInfo* pExecuteInfo = new ExecuteInfo;

            pExecuteInfo->bRbtEditLater = m_xRbtEditLater->get_active();
            pExecuteInfo->bRbtEditNow = m_xRbtEditNow->get_active();
            // get private-url
            sal_Int32 nPos = m_xLbDocTypes->get_selected_index();
            if (nPos == -1)
                nPos = 0;
            pExecuteInfo->aURL = aURL;
            pExecuteInfo->aStrDocName = reinterpret_cast<DocumentTypeData*>(m_xLbDocTypes->get_id(nPos).toInt64())->aStrURL;

            // current document
            pExecuteInfo->xFrame = GetDispatcher()->GetFrame()->GetFrame().GetFrameInterface();
            pExecuteInfo->pDispatcher = GetDispatcher();

            Application::PostUserEvent(LINK(nullptr, SvxHyperlinkNewDocTp, DispatchDocument), pExecuteInfo);
        }
    }
}

/*************************************************************************
|*
|* Click on imagebutton : new
|*
|************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkNewDocTp, ClickNewHdl_Impl, weld::Button&, void)
{
    DisableClose( true );
    uno::Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
    uno::Reference < XFolderPicker2 >  xFolderPicker = FolderPicker::create(xContext);

    OUString            aStrURL;
    OUString            aTempStrURL( m_xCbbPath->get_active_text() );
    osl::FileBase::getFileURLFromSystemPath( aTempStrURL, aStrURL );

    OUString            aStrPath = aStrURL;
    bool            bZeroPath = aStrPath.isEmpty();
    bool            bHandleFileName = bZeroPath;    // when path has length of 0, then the rest should always be handled
                                                        //  as file name, otherwise we do not yet know

    if( bZeroPath )
        aStrPath = SvtPathOptions().GetWorkPath();
    else if( !::utl::UCBContentHelper::IsFolder( aStrURL ) )
        bHandleFileName = true;

    xFolderPicker->setDisplayDirectory( aStrPath );
    sal_Int16 nResult = xFolderPicker->execute();
    DisableClose( false );
    if( ExecutableDialogResults::OK == nResult )
    {
        sal_Char const  sSlash[] = "/";

        INetURLObject   aURL( aStrURL, INetProtocol::File );
        OUString        aStrName;
        if( bHandleFileName )
            aStrName = bZeroPath? aTempStrURL : aURL.getName();

        m_xCbbPath->SetBaseURL( xFolderPicker->getDirectory() );
        OUString          aStrTmp( xFolderPicker->getDirectory() );

        if( aStrTmp[ aStrTmp.getLength() - 1 ] != sSlash[0] )
            aStrTmp += sSlash;

        // append old file name
        if( bHandleFileName )
            aStrTmp += aStrName;

        INetURLObject   aNewURL( aStrTmp );

        if (!aStrName.isEmpty() && !aNewURL.getExtension().isEmpty() &&
           m_xLbDocTypes->get_selected_index() != -1)
        {
            // get private-url
            const sal_Int32 nPos = m_xLbDocTypes->get_selected_index();
            aNewURL.setExtension(reinterpret_cast<DocumentTypeData*>(m_xLbDocTypes->get_id(nPos).toInt64())->aStrExt);
        }

        if( aNewURL.GetProtocol() == INetProtocol::File )
        {
            osl::FileBase::getSystemPathFromFileURL(aNewURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aStrTmp);
        }
        else
        {
            aStrTmp = aNewURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous );
        }

        m_xCbbPath->set_entry_text( aStrTmp );
    }
}

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