1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <comphelper/processfactory.hxx>
11 #include <comphelper/string.hxx>
12 #include <comphelper/storagehelper.hxx>
13 #include <sfx2/sfxresid.hxx>
14 #include <sfx2/app.hxx>
15 #include <sfx2/fcontnr.hxx>
16 #include <sfx2/docfac.hxx>
17 #include <sfx2/doctempl.hxx>
18 #include <sfx2/docfilt.hxx>
20 #include <vcl/svapp.hxx>
21 #include <vcl/weld.hxx>
22 #include <sot/storage.hxx>
24 #include <com/sun/star/frame/DocumentTemplates.hpp>
25 #include <com/sun/star/frame/XStorable.hpp>
27 #include <sfx2/strings.hrc>
29 #include <saveastemplatedlg.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::frame
;
34 // Class SfxSaveAsTemplateDialog --------------------------------------------------
36 SfxSaveAsTemplateDialog::SfxSaveAsTemplateDialog(weld::Window
* pParent
, uno::Reference
<frame::XModel
> xModel
)
37 : GenericDialogController(pParent
, u
"sfx/ui/saveastemplatedlg.ui"_ustr
, u
"SaveAsTemplateDialog"_ustr
)
38 , m_xLBCategory(m_xBuilder
->weld_tree_view(u
"categorylb"_ustr
))
39 , m_xCBXDefault(m_xBuilder
->weld_check_button(u
"defaultcb"_ustr
))
40 , m_xTemplateNameEdit(m_xBuilder
->weld_entry(u
"name_entry"_ustr
))
41 , m_xOKButton(m_xBuilder
->weld_button(u
"ok"_ustr
))
43 , m_xModel(std::move(xModel
))
45 m_xLBCategory
->append_text(SfxResId(STR_CATEGORY_NONE
));
47 SetCategoryLBEntries(msCategories
);
49 m_xTemplateNameEdit
->connect_changed(LINK(this, SfxSaveAsTemplateDialog
, TemplateNameEditHdl
));
50 m_xLBCategory
->connect_selection_changed(
51 LINK(this, SfxSaveAsTemplateDialog
, SelectCategoryHdl
));
52 m_xLBCategory
->set_size_request(m_xLBCategory
->get_approximate_digit_width() * 32,
53 m_xLBCategory
->get_height_rows(8));
54 m_xOKButton
->connect_clicked(LINK(this, SfxSaveAsTemplateDialog
, OkClickHdl
));
56 m_xOKButton
->set_sensitive(false);
57 m_xOKButton
->set_label(SfxResId(STR_SAVEDOC
));
60 IMPL_LINK_NOARG(SfxSaveAsTemplateDialog
, OkClickHdl
, weld::Button
&, void)
62 std::unique_ptr
<weld::MessageDialog
> xQueryDlg(Application::CreateMessageDialog(m_xDialog
.get(), VclMessageType::Question
,
63 VclButtonsType::YesNo
, OUString()));
64 if(!IsTemplateNameUnique())
66 OUString
sQueryMsg(SfxResId(STR_QMSG_TEMPLATE_OVERWRITE
));
67 sQueryMsg
= sQueryMsg
.replaceFirst("$1",msTemplateName
);
68 xQueryDlg
->set_primary_text(sQueryMsg
.replaceFirst("$2", msSelectedCategory
));
70 if (xQueryDlg
->run() == RET_NO
)
75 m_xDialog
->response(RET_OK
);
78 OUString
sText( SfxResId(STR_ERROR_SAVEAS
) );
79 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(m_xDialog
.get(), VclMessageType::Warning
,
80 VclButtonsType::Ok
, sText
.replaceFirst("$1", msTemplateName
)));
85 IMPL_LINK_NOARG(SfxSaveAsTemplateDialog
, TemplateNameEditHdl
, weld::Entry
&, void)
87 msTemplateName
= comphelper::string::strip(m_xTemplateNameEdit
->get_text(), ' ');
88 SelectCategoryHdl(*m_xLBCategory
);
91 IMPL_LINK_NOARG(SfxSaveAsTemplateDialog
, SelectCategoryHdl
, weld::TreeView
&, void)
93 if (m_xLBCategory
->get_selected_index() == 0)
95 msSelectedCategory
= OUString();
96 m_xOKButton
->set_sensitive(false);
100 msSelectedCategory
= m_xLBCategory
->get_selected_text();
101 m_xOKButton
->set_sensitive(!msTemplateName
.isEmpty());
105 void SfxSaveAsTemplateDialog::initialize()
107 sal_uInt16 nCount
= maDocTemplates
.GetRegionCount();
108 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
110 OUString
sCategoryName(maDocTemplates
.GetFullRegionName(i
));
111 msCategories
.push_back(sCategoryName
);
115 void SfxSaveAsTemplateDialog::SetCategoryLBEntries(const std::vector
<OUString
>& rFolderNames
)
117 for (size_t i
= 0, n
= rFolderNames
.size(); i
< n
; ++i
)
118 m_xLBCategory
->insert_text(i
+1, rFolderNames
[i
]);
119 m_xLBCategory
->select(0);
122 bool SfxSaveAsTemplateDialog::IsTemplateNameUnique()
124 std::vector
<OUString
>::iterator it
=find(msCategories
.begin(), msCategories
.end(), msSelectedCategory
);
125 mnRegionPos
= std::distance(msCategories
.begin(), it
);
127 sal_uInt16 nEntries
= maDocTemplates
.GetCount(mnRegionPos
);
128 for(sal_uInt16 i
= 0; i
< nEntries
; i
++)
130 OUString aName
= maDocTemplates
.GetName(mnRegionPos
, i
);
131 if(aName
== msTemplateName
)
138 bool SfxSaveAsTemplateDialog::SaveTemplate()
140 uno::Reference
< frame::XStorable
> xStorable(m_xModel
, uno::UNO_QUERY_THROW
);
142 uno::Reference
< frame::XDocumentTemplates
> xTemplates(frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) );
144 if (!xTemplates
->storeTemplate( msSelectedCategory
, msTemplateName
, xStorable
))
147 sal_uInt16 nDocId
= maDocTemplates
.GetCount(mnRegionPos
);
148 OUString sURL
= maDocTemplates
.GetTemplateTargetURLFromComponent(msSelectedCategory
, msTemplateName
);
149 bool bIsSaved
= maDocTemplates
.InsertTemplate( mnRegionPos
, nDocId
, msTemplateName
, sURL
);
154 if (!sURL
.isEmpty() && m_xCBXDefault
->get_active())
156 OUString aServiceName
;
159 uno::Reference
< embed::XStorage
> xStorage
=
160 comphelper::OStorageHelper::GetStorageFromURL( sURL
, embed::ElementModes::READ
);
162 SotClipboardFormatId nFormat
= SotStorage::GetFormatID( xStorage
);
164 std::shared_ptr
<const SfxFilter
> pFilter
= SfxGetpApp()->GetFilterMatcher().GetFilter4ClipBoardId( nFormat
);
167 aServiceName
= pFilter
->GetServiceName();
169 catch( uno::Exception
& )
172 if(!aServiceName
.isEmpty())
173 SfxObjectFactory::SetStandardTemplate(aServiceName
, sURL
);
176 maDocTemplates
.Update();
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */