/src/libreoffice/sfx2/source/doc/saveastemplatedlg.cxx
Line | Count | Source |
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 | | |
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 <utility> |
20 | | #include <vcl/svapp.hxx> |
21 | | #include <vcl/vclenum.hxx> |
22 | | #include <vcl/weld/Builder.hxx> |
23 | | #include <vcl/weld/Dialog.hxx> |
24 | | #include <vcl/weld/MessageDialog.hxx> |
25 | | #include <sot/storage.hxx> |
26 | | |
27 | | #include <com/sun/star/frame/DocumentTemplates.hpp> |
28 | | #include <com/sun/star/frame/XStorable.hpp> |
29 | | |
30 | | #include <sfx2/strings.hrc> |
31 | | |
32 | | #include <saveastemplatedlg.hxx> |
33 | | |
34 | | using namespace ::com::sun::star; |
35 | | using namespace ::com::sun::star::frame; |
36 | | |
37 | | // Class SfxSaveAsTemplateDialog -------------------------------------------------- |
38 | | |
39 | | SfxSaveAsTemplateDialog::SfxSaveAsTemplateDialog(weld::Window* pParent, uno::Reference<frame::XModel> xModel) |
40 | 0 | : GenericDialogController(pParent, u"sfx/ui/saveastemplatedlg.ui"_ustr, u"SaveAsTemplateDialog"_ustr) |
41 | 0 | , m_xLBCategory(m_xBuilder->weld_tree_view(u"categorylb"_ustr)) |
42 | 0 | , m_xCBXDefault(m_xBuilder->weld_check_button(u"defaultcb"_ustr)) |
43 | 0 | , m_xTemplateNameEdit(m_xBuilder->weld_entry(u"name_entry"_ustr)) |
44 | 0 | , m_xOKButton(m_xBuilder->weld_button(u"ok"_ustr)) |
45 | 0 | , mnRegionPos(0) |
46 | 0 | , m_xModel(std::move(xModel)) |
47 | 0 | { |
48 | 0 | m_xLBCategory->append_text(SfxResId(STR_CATEGORY_NONE)); |
49 | 0 | initialize(); |
50 | 0 | SetCategoryLBEntries(msCategories); |
51 | |
|
52 | 0 | m_xTemplateNameEdit->connect_changed(LINK(this, SfxSaveAsTemplateDialog, TemplateNameEditHdl)); |
53 | 0 | m_xLBCategory->connect_selection_changed( |
54 | 0 | LINK(this, SfxSaveAsTemplateDialog, SelectCategoryHdl)); |
55 | 0 | m_xLBCategory->set_size_request(m_xLBCategory->get_approximate_digit_width() * 32, |
56 | 0 | m_xLBCategory->get_height_rows(8)); |
57 | 0 | m_xOKButton->connect_clicked(LINK(this, SfxSaveAsTemplateDialog, OkClickHdl)); |
58 | |
|
59 | 0 | m_xOKButton->set_sensitive(false); |
60 | 0 | m_xOKButton->set_label(SfxResId(STR_SAVEDOC)); |
61 | 0 | } |
62 | | |
63 | | IMPL_LINK_NOARG(SfxSaveAsTemplateDialog, OkClickHdl, weld::Button&, void) |
64 | 0 | { |
65 | 0 | std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Question, |
66 | 0 | VclButtonsType::YesNo, OUString())); |
67 | 0 | if(!IsTemplateNameUnique()) |
68 | 0 | { |
69 | 0 | OUString sQueryMsg(SfxResId(STR_QMSG_TEMPLATE_OVERWRITE)); |
70 | 0 | sQueryMsg = sQueryMsg.replaceFirst("$1",msTemplateName); |
71 | 0 | xQueryDlg->set_primary_text(sQueryMsg.replaceFirst("$2", msSelectedCategory)); |
72 | |
|
73 | 0 | if (xQueryDlg->run() == RET_NO) |
74 | 0 | return; |
75 | 0 | } |
76 | | |
77 | 0 | if (SaveTemplate()) |
78 | 0 | m_xDialog->response(RET_OK); |
79 | 0 | else |
80 | 0 | { |
81 | 0 | OUString sText( SfxResId(STR_ERROR_SAVEAS) ); |
82 | 0 | std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Warning, |
83 | 0 | VclButtonsType::Ok, sText.replaceFirst("$1", msTemplateName))); |
84 | 0 | xBox->run(); |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | IMPL_LINK_NOARG(SfxSaveAsTemplateDialog, TemplateNameEditHdl, weld::TextWidget&, void) |
89 | 0 | { |
90 | 0 | msTemplateName = comphelper::string::strip(m_xTemplateNameEdit->get_text(), ' '); |
91 | 0 | SelectCategoryHdl(*m_xLBCategory); |
92 | 0 | } |
93 | | |
94 | | IMPL_LINK_NOARG(SfxSaveAsTemplateDialog, SelectCategoryHdl, weld::ItemView&, void) |
95 | 0 | { |
96 | 0 | if (m_xLBCategory->get_selected_index() == 0) |
97 | 0 | { |
98 | 0 | msSelectedCategory = OUString(); |
99 | 0 | m_xOKButton->set_sensitive(false); |
100 | 0 | } |
101 | 0 | else |
102 | 0 | { |
103 | 0 | msSelectedCategory = m_xLBCategory->get_selected_text(); |
104 | 0 | m_xOKButton->set_sensitive(!msTemplateName.isEmpty()); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | void SfxSaveAsTemplateDialog::initialize() |
109 | 0 | { |
110 | 0 | sal_uInt16 nCount = maDocTemplates.GetRegionCount(); |
111 | 0 | for (sal_uInt16 i = 0; i < nCount; ++i) |
112 | 0 | { |
113 | 0 | OUString sCategoryName(maDocTemplates.GetFullRegionName(i)); |
114 | 0 | msCategories.push_back(sCategoryName); |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | void SfxSaveAsTemplateDialog::SetCategoryLBEntries(const std::vector<OUString>& rFolderNames) |
119 | 0 | { |
120 | 0 | for (size_t i = 0, n = rFolderNames.size(); i < n; ++i) |
121 | 0 | m_xLBCategory->insert_text(i+1, rFolderNames[i]); |
122 | 0 | m_xLBCategory->select(0); |
123 | 0 | } |
124 | | |
125 | | bool SfxSaveAsTemplateDialog::IsTemplateNameUnique() |
126 | 0 | { |
127 | 0 | std::vector<OUString>::iterator it=find(msCategories.begin(), msCategories.end(), msSelectedCategory); |
128 | 0 | mnRegionPos = std::distance(msCategories.begin(), it); |
129 | |
|
130 | 0 | sal_uInt16 nEntries = maDocTemplates.GetCount(mnRegionPos); |
131 | 0 | for(sal_uInt16 i = 0; i < nEntries; i++) |
132 | 0 | { |
133 | 0 | OUString aName = maDocTemplates.GetName(mnRegionPos, i); |
134 | 0 | if(aName == msTemplateName) |
135 | 0 | return false; |
136 | 0 | } |
137 | | |
138 | 0 | return true; |
139 | 0 | } |
140 | | |
141 | | bool SfxSaveAsTemplateDialog::SaveTemplate() |
142 | 0 | { |
143 | 0 | uno::Reference< frame::XStorable > xStorable(m_xModel, uno::UNO_QUERY_THROW ); |
144 | |
|
145 | 0 | uno::Reference< frame::XDocumentTemplates > xTemplates(frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) ); |
146 | |
|
147 | 0 | if (!xTemplates->storeTemplate( msSelectedCategory, msTemplateName, xStorable )) |
148 | 0 | return false; |
149 | | |
150 | 0 | sal_uInt16 nDocId = maDocTemplates.GetCount(mnRegionPos); |
151 | 0 | OUString sURL = maDocTemplates.GetTemplateTargetURLFromComponent(msSelectedCategory, msTemplateName); |
152 | 0 | bool bIsSaved = maDocTemplates.InsertTemplate( mnRegionPos, nDocId, msTemplateName, sURL); |
153 | |
|
154 | 0 | if (!bIsSaved) |
155 | 0 | return false; |
156 | | |
157 | 0 | if (!sURL.isEmpty() && m_xCBXDefault->get_active()) |
158 | 0 | { |
159 | 0 | OUString aServiceName; |
160 | 0 | try |
161 | 0 | { |
162 | 0 | uno::Reference< embed::XStorage > xStorage = |
163 | 0 | comphelper::OStorageHelper::GetStorageFromURL( sURL, embed::ElementModes::READ ); |
164 | |
|
165 | 0 | SotClipboardFormatId nFormat = SotStorage::GetFormatID( xStorage ); |
166 | |
|
167 | 0 | std::shared_ptr<const SfxFilter> pFilter = SfxGetpApp()->GetFilterMatcher().GetFilter4ClipBoardId( nFormat ); |
168 | |
|
169 | 0 | if ( pFilter ) |
170 | 0 | aServiceName = pFilter->GetServiceName(); |
171 | 0 | } |
172 | 0 | catch( uno::Exception& ) |
173 | 0 | {} |
174 | |
|
175 | 0 | if(!aServiceName.isEmpty()) |
176 | 0 | SfxObjectFactory::SetStandardTemplate(aServiceName, sURL); |
177 | 0 | } |
178 | |
|
179 | 0 | maDocTemplates.Update(); |
180 | 0 | return true; |
181 | 0 | } |
182 | | |
183 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |