LibreOffice Module sfx2 (master) 1
saveastemplatedlg.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
11#include <comphelper/string.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 <utility>
20#include <vcl/svapp.hxx>
21#include <vcl/weld.hxx>
22#include <sot/storage.hxx>
23
24#include <com/sun/star/frame/DocumentTemplates.hpp>
25#include <com/sun/star/frame/XStorable.hpp>
26
27#include <sfx2/strings.hrc>
28
29#include <saveastemplatedlg.hxx>
30
31using namespace ::com::sun::star;
32using namespace ::com::sun::star::frame;
33
34// Class SfxSaveAsTemplateDialog --------------------------------------------------
35
36SfxSaveAsTemplateDialog::SfxSaveAsTemplateDialog(weld::Window* pParent, uno::Reference<frame::XModel> xModel)
37 : GenericDialogController(pParent, "sfx/ui/saveastemplatedlg.ui", "SaveAsTemplateDialog")
38 , m_xLBCategory(m_xBuilder->weld_tree_view("categorylb"))
39 , m_xCBXDefault(m_xBuilder->weld_check_button("defaultcb"))
40 , m_xTemplateNameEdit(m_xBuilder->weld_entry("name_entry"))
41 , m_xOKButton(m_xBuilder->weld_button("ok"))
42 , mnRegionPos(0)
43 , m_xModel(std::move(xModel))
44{
45 m_xLBCategory->append_text(SfxResId(STR_CATEGORY_NONE));
46 initialize();
48
49 m_xTemplateNameEdit->connect_changed(LINK(this, SfxSaveAsTemplateDialog, TemplateNameEditHdl));
50 m_xLBCategory->connect_changed(LINK(this, SfxSaveAsTemplateDialog, SelectCategoryHdl));
51 m_xLBCategory->set_size_request(m_xLBCategory->get_approximate_digit_width() * 32,
52 m_xLBCategory->get_height_rows(8));
53 m_xOKButton->connect_clicked(LINK(this, SfxSaveAsTemplateDialog, OkClickHdl));
54
55 m_xOKButton->set_sensitive(false);
56 m_xOKButton->set_label(SfxResId(STR_SAVEDOC));
57}
58
60{
61 std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Question,
62 VclButtonsType::YesNo, OUString()));
63 if(!IsTemplateNameUnique())
64 {
65 OUString sQueryMsg(SfxResId(STR_QMSG_TEMPLATE_OVERWRITE));
66 sQueryMsg = sQueryMsg.replaceFirst("$1",msTemplateName);
67 xQueryDlg->set_primary_text(sQueryMsg.replaceFirst("$2", msSelectedCategory));
68
69 if (xQueryDlg->run() == RET_NO)
70 return;
71 }
72
73 if (SaveTemplate())
74 m_xDialog->response(RET_OK);
75 else
76 {
77 OUString sText( SfxResId(STR_ERROR_SAVEAS) );
78 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Warning,
79 VclButtonsType::Ok, sText.replaceFirst("$1", msTemplateName)));
80 xBox->run();
81 }
82}
83
85{
86 msTemplateName = comphelper::string::strip(m_xTemplateNameEdit->get_text(), ' ');
87 SelectCategoryHdl(*m_xLBCategory);
88}
89
91{
92 if (m_xLBCategory->get_selected_index() == 0)
93 {
94 msSelectedCategory = OUString();
95 m_xOKButton->set_sensitive(false);
96 }
97 else
98 {
99 msSelectedCategory = m_xLBCategory->get_selected_text();
100 m_xOKButton->set_sensitive(!msTemplateName.isEmpty());
101 }
102}
103
105{
106 sal_uInt16 nCount = maDocTemplates.GetRegionCount();
107 for (sal_uInt16 i = 0; i < nCount; ++i)
108 {
109 OUString sCategoryName(maDocTemplates.GetFullRegionName(i));
110 msCategories.push_back(sCategoryName);
111 }
112}
113
114void SfxSaveAsTemplateDialog::SetCategoryLBEntries(const std::vector<OUString>& rFolderNames)
115{
116 for (size_t i = 0, n = rFolderNames.size(); i < n; ++i)
117 m_xLBCategory->insert_text(i+1, rFolderNames[i]);
118 m_xLBCategory->select(0);
119}
120
122{
123 std::vector<OUString>::iterator it=find(msCategories.begin(), msCategories.end(), msSelectedCategory);
124 mnRegionPos = std::distance(msCategories.begin(), it);
125
126 sal_uInt16 nEntries = maDocTemplates.GetCount(mnRegionPos);
127 for(sal_uInt16 i = 0; i < nEntries; i++)
128 {
130 if(aName == msTemplateName)
131 return false;
132 }
133
134 return true;
135}
136
138{
139 uno::Reference< frame::XStorable > xStorable(m_xModel, uno::UNO_QUERY_THROW );
140
141 uno::Reference< frame::XDocumentTemplates > xTemplates(frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) );
142
143 if (!xTemplates->storeTemplate( msSelectedCategory, msTemplateName, xStorable ))
144 return false;
145
146 sal_uInt16 nDocId = maDocTemplates.GetCount(mnRegionPos);
148 bool bIsSaved = maDocTemplates.InsertTemplate( mnRegionPos, nDocId, msTemplateName, sURL);
149
150 if (!bIsSaved)
151 return false;
152
153 if (!sURL.isEmpty() && m_xCBXDefault->get_active())
154 {
155 OUString aServiceName;
156 try
157 {
158 uno::Reference< embed::XStorage > xStorage =
159 comphelper::OStorageHelper::GetStorageFromURL( sURL, embed::ElementModes::READ );
160
161 SotClipboardFormatId nFormat = SotStorage::GetFormatID( xStorage );
162
163 std::shared_ptr<const SfxFilter> pFilter = SfxGetpApp()->GetFilterMatcher().GetFilter4ClipBoardId( nFormat );
164
165 if ( pFilter )
166 aServiceName = pFilter->GetServiceName();
167 }
168 catch( uno::Exception& )
169 {}
170
171 if(!aServiceName.isEmpty())
172 SfxObjectFactory::SetStandardTemplate(aServiceName, sURL);
173 }
174
176 return true;
177}
178
179/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxApplication * SfxGetpApp()
Definition: app.hxx:231
Reference< XExecutableDialog > m_xDialog
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
SfxFilterMatcher & GetFilterMatcher()
Definition: appmain.cxx:27
bool InsertTemplate(sal_uInt16 nSourceRegion, sal_uInt16 nIdx, const OUString &rName, const OUString &rPath)
Definition: doctempl.cxx:1026
void Update()
updates the configuration where the document templates structure is stored.
Definition: doctempl.cxx:1237
sal_uInt16 GetRegionCount() const
Definition: doctempl.cxx:313
sal_uInt16 GetCount(sal_uInt16 nRegion) const
Definition: doctempl.cxx:334
OUString GetFullRegionName(sal_uInt16 nIdx) const
Definition: doctempl.cxx:251
OUString GetTemplateTargetURLFromComponent(std::u16string_view aGroupName, std::u16string_view aTitle)
Definition: doctempl.cxx:429
OUString GetName(sal_uInt16 nRegion, sal_uInt16 nIdx) const
Definition: doctempl.cxx:363
std::shared_ptr< const SfxFilter > GetFilter4ClipBoardId(SotClipboardFormatId nId, SfxFilterFlags nMust=SfxFilterFlags::IMPORT, SfxFilterFlags nDont=SFX_FILTER_NOTINSTALLED) const
Definition: fltfnc.cxx:699
static void SetStandardTemplate(const OUString &rServiceName, const OUString &rTemplateName)
Definition: docfac.cxx:239
std::unique_ptr< weld::TreeView > m_xLBCategory
std::vector< OUString > msCategories
SfxDocumentTemplates maDocTemplates
std::unique_ptr< weld::CheckButton > m_xCBXDefault
void SetCategoryLBEntries(const std::vector< OUString > &names)
std::unique_ptr< weld::Entry > m_xTemplateNameEdit
css::uno::Reference< css::frame::XModel > m_xModel
std::unique_ptr< weld::Button > m_xOKButton
SfxSaveAsTemplateDialog(weld::Window *pParent, css::uno::Reference< css::frame::XModel > xModel)
static SotClipboardFormatId GetFormatID(css::uno::Reference< css::embed::XStorage > const &xStorage)
static css::uno::Reference< css::embed::XStorage > GetStorageFromURL(const OUString &aURL, sal_Int32 nStorageMode, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
int nCount
Reference< frame::XModel > m_xModel
SotClipboardFormatId
std::unique_ptr< weld::Button > m_xOKButton
Definition: impldde.cxx:53
OUString aName
sal_Int64 n
OString strip(const OString &rIn, char c)
Reference< XComponentContext > getProcessComponentContext()
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)
int i
IMPL_LINK_NOARG(SfxSaveAsTemplateDialog, OkClickHdl, weld::Button &, void)
OUString SfxResId(TranslateId aId)
Definition: sfxresid.cxx:22
Reference< XModel > xModel
RET_OK
RET_NO