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>
19 #include <vcl/svapp.hxx>
20 #include <vcl/weld.hxx>
21 #include <sot/storage.hxx>
23 #include <com/sun/star/frame/DocumentTemplates.hpp>
24 #include <com/sun/star/frame/XStorable.hpp>
26 #include <sfx2/strings.hrc>
28 #include <saveastemplatedlg.hxx>
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::frame
;
33 // Class SfxSaveAsTemplateDialog --------------------------------------------------
35 SfxSaveAsTemplateDialog::SfxSaveAsTemplateDialog(weld::Window
* pParent
, const uno::Reference
<frame::XModel
> &rModel
)
36 : GenericDialogController(pParent
, "sfx/ui/saveastemplatedlg.ui", "SaveAsTemplateDialog")
37 , m_xLBCategory(m_xBuilder
->weld_tree_view("categorylb"))
38 , m_xCBXDefault(m_xBuilder
->weld_check_button("defaultcb"))
39 , m_xTemplateNameEdit(m_xBuilder
->weld_entry("name_entry"))
40 , m_xOKButton(m_xBuilder
->weld_button("ok"))
44 m_xLBCategory
->append_text(SfxResId(STR_CATEGORY_NONE
));
46 SetCategoryLBEntries(msCategories
);
48 m_xTemplateNameEdit
->connect_changed(LINK(this, SfxSaveAsTemplateDialog
, TemplateNameEditHdl
));
49 m_xLBCategory
->connect_changed(LINK(this, SfxSaveAsTemplateDialog
, SelectCategoryHdl
));
50 m_xLBCategory
->set_size_request(m_xLBCategory
->get_approximate_digit_width() * 32,
51 m_xLBCategory
->get_height_rows(8));
52 m_xOKButton
->connect_clicked(LINK(this, SfxSaveAsTemplateDialog
, OkClickHdl
));
54 m_xOKButton
->set_sensitive(false);
55 m_xOKButton
->set_label(SfxResId(STR_SAVEDOC
));
58 IMPL_LINK_NOARG(SfxSaveAsTemplateDialog
, OkClickHdl
, weld::Button
&, void)
60 std::unique_ptr
<weld::MessageDialog
> xQueryDlg(Application::CreateMessageDialog(m_xDialog
.get(), VclMessageType::Question
,
61 VclButtonsType::YesNo
, OUString()));
62 if(!IsTemplateNameUnique())
64 OUString
sQueryMsg(SfxResId(STR_QMSG_TEMPLATE_OVERWRITE
));
65 sQueryMsg
= sQueryMsg
.replaceFirst("$1",msTemplateName
);
66 xQueryDlg
->set_primary_text(sQueryMsg
.replaceFirst("$2", msSelectedCategory
));
68 if (xQueryDlg
->run() == RET_NO
)
73 m_xDialog
->response(RET_OK
);
76 OUString
sText( SfxResId(STR_ERROR_SAVEAS
) );
77 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(m_xDialog
.get(), VclMessageType::Warning
,
78 VclButtonsType::Ok
, sText
.replaceFirst("$1", msTemplateName
)));
83 IMPL_LINK_NOARG(SfxSaveAsTemplateDialog
, TemplateNameEditHdl
, weld::Entry
&, void)
85 msTemplateName
= comphelper::string::strip(m_xTemplateNameEdit
->get_text(), ' ');
86 SelectCategoryHdl(*m_xLBCategory
);
89 IMPL_LINK_NOARG(SfxSaveAsTemplateDialog
, SelectCategoryHdl
, weld::TreeView
&, void)
91 if (m_xLBCategory
->get_selected_index() == 0)
93 msSelectedCategory
= OUString();
94 m_xOKButton
->set_sensitive(false);
98 msSelectedCategory
= m_xLBCategory
->get_selected_text();
99 m_xOKButton
->set_sensitive(!msTemplateName
.isEmpty());
103 void SfxSaveAsTemplateDialog::initialize()
105 sal_uInt16 nCount
= maDocTemplates
.GetRegionCount();
106 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
108 OUString
sCategoryName(maDocTemplates
.GetFullRegionName(i
));
109 msCategories
.push_back(sCategoryName
);
113 void SfxSaveAsTemplateDialog::SetCategoryLBEntries(const std::vector
<OUString
>& rFolderNames
)
115 for (size_t i
= 0, n
= rFolderNames
.size(); i
< n
; ++i
)
116 m_xLBCategory
->insert_text(i
+1, rFolderNames
[i
]);
117 m_xLBCategory
->select(0);
120 bool SfxSaveAsTemplateDialog::IsTemplateNameUnique()
122 std::vector
<OUString
>::iterator it
=find(msCategories
.begin(), msCategories
.end(), msSelectedCategory
);
123 mnRegionPos
= std::distance(msCategories
.begin(), it
);
125 sal_uInt16 nEntries
= maDocTemplates
.GetCount(mnRegionPos
);
126 for(sal_uInt16 i
= 0; i
< nEntries
; i
++)
128 OUString aName
= maDocTemplates
.GetName(mnRegionPos
, i
);
129 if(aName
== msTemplateName
)
136 bool SfxSaveAsTemplateDialog::SaveTemplate()
138 uno::Reference
< frame::XStorable
> xStorable(m_xModel
, uno::UNO_QUERY_THROW
);
140 uno::Reference
< frame::XDocumentTemplates
> xTemplates(frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) );
142 if (!xTemplates
->storeTemplate( msSelectedCategory
, msTemplateName
, xStorable
))
145 sal_uInt16 nDocId
= maDocTemplates
.GetCount(mnRegionPos
);
146 OUString sURL
= maDocTemplates
.GetTemplateTargetURLFromComponent(msSelectedCategory
, msTemplateName
);
147 bool bIsSaved
= maDocTemplates
.InsertTemplate( mnRegionPos
, nDocId
, msTemplateName
, sURL
);
152 if (!sURL
.isEmpty() && m_xCBXDefault
->get_active())
154 OUString aServiceName
;
157 uno::Reference
< embed::XStorage
> xStorage
=
158 comphelper::OStorageHelper::GetStorageFromURL( sURL
, embed::ElementModes::READ
);
160 SotClipboardFormatId nFormat
= SotStorage::GetFormatID( xStorage
);
162 std::shared_ptr
<const SfxFilter
> pFilter
= SfxGetpApp()->GetFilterMatcher().GetFilter4ClipBoardId( nFormat
);
165 aServiceName
= pFilter
->GetServiceName();
167 catch( uno::Exception
& )
170 if(!aServiceName
.isEmpty())
171 SfxObjectFactory::SetStandardTemplate(aServiceName
, sURL
);
174 maDocTemplates
.Update();
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */