Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / doc / saveastemplatedlg.cxx
bloba17ca283ed8f09a376f88a567d75ed4c763c2b08
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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"))
41 , msSelectedCategory(OUString())
42 , msTemplateName(OUString())
43 , mnRegionPos(0)
44 , m_xModel(rModel)
46 m_xLBCategory->append_text(SfxResId(STR_CATEGORY_NONE));
47 initialize();
48 SetCategoryLBEntries(msCategories);
50 m_xTemplateNameEdit->connect_changed(LINK(this, SfxSaveAsTemplateDialog, TemplateNameEditHdl));
51 m_xLBCategory->connect_changed(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)
71 return;
74 if (SaveTemplate())
75 m_xDialog->response(RET_OK);
76 else
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)));
81 xBox->run();
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);
98 else
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)
132 return false;
135 return true;
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 ))
145 return false;
147 sal_uInt16 nDocId = maDocTemplates.GetCount(mnRegionPos);
148 OUString sURL = maDocTemplates.GetTemplateTargetURLFromComponent(msSelectedCategory, msTemplateName);
149 bool bIsSaved = maDocTemplates.InsertTemplate( mnRegionPos, nDocId, msTemplateName, sURL);
151 if (!bIsSaved)
152 return false;
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 );
166 if ( pFilter )
167 aServiceName = pFilter->GetServiceName();
169 catch( uno::Exception& )
172 if(!aServiceName.isEmpty())
173 SfxObjectFactory::SetStandardTemplate(aServiceName, sURL);
176 maDocTemplates.Update();
177 return true;
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */