LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / saveastemplatedlg.cxx
blob6f9fd1b4757f4b05e3f2af4445f34b1f16a47e2f
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 , mnRegionPos(0)
42 , m_xModel(rModel)
44 m_xLBCategory->append_text(SfxResId(STR_CATEGORY_NONE));
45 initialize();
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)
69 return;
72 if (SaveTemplate())
73 m_xDialog->response(RET_OK);
74 else
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)));
79 xBox->run();
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);
96 else
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)
130 return false;
133 return true;
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 ))
143 return false;
145 sal_uInt16 nDocId = maDocTemplates.GetCount(mnRegionPos);
146 OUString sURL = maDocTemplates.GetTemplateTargetURLFromComponent(msSelectedCategory, msTemplateName);
147 bool bIsSaved = maDocTemplates.InsertTemplate( mnRegionPos, nDocId, msTemplateName, sURL);
149 if (!bIsSaved)
150 return false;
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 );
164 if ( pFilter )
165 aServiceName = pFilter->GetServiceName();
167 catch( uno::Exception& )
170 if(!aServiceName.isEmpty())
171 SfxObjectFactory::SetStandardTemplate(aServiceName, sURL);
174 maDocTemplates.Update();
175 return true;
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */