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