/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace ::com::sun::star; using namespace ::com::sun::star::frame; // Class SfxSaveAsTemplateDialog -------------------------------------------------- SfxSaveAsTemplateDialog::SfxSaveAsTemplateDialog(weld::Window* pParent, uno::Reference xModel) : GenericDialogController(pParent, "sfx/ui/saveastemplatedlg.ui", "SaveAsTemplateDialog") , m_xLBCategory(m_xBuilder->weld_tree_view("categorylb")) , m_xCBXDefault(m_xBuilder->weld_check_button("defaultcb")) , m_xTemplateNameEdit(m_xBuilder->weld_entry("name_entry")) , m_xOKButton(m_xBuilder->weld_button("ok")) , mnRegionPos(0) , m_xModel(std::move(xModel)) { m_xLBCategory->append_text(SfxResId(STR_CATEGORY_NONE)); initialize(); SetCategoryLBEntries(msCategories); m_xTemplateNameEdit->connect_changed(LINK(this, SfxSaveAsTemplateDialog, TemplateNameEditHdl)); m_xLBCategory->connect_changed(LINK(this, SfxSaveAsTemplateDialog, SelectCategoryHdl)); m_xLBCategory->set_size_request(m_xLBCategory->get_approximate_digit_width() * 32, m_xLBCategory->get_height_rows(8)); m_xOKButton->connect_clicked(LINK(this, SfxSaveAsTemplateDialog, OkClickHdl)); m_xOKButton->set_sensitive(false); m_xOKButton->set_label(SfxResId(STR_SAVEDOC)); } IMPL_LINK_NOARG(SfxSaveAsTemplateDialog, OkClickHdl, weld::Button&, void) { std::unique_ptr xQueryDlg(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Question, VclButtonsType::YesNo, OUString())); if(!IsTemplateNameUnique()) { OUString sQueryMsg(SfxResId(STR_QMSG_TEMPLATE_OVERWRITE)); sQueryMsg = sQueryMsg.replaceFirst("$1",msTemplateName); xQueryDlg->set_primary_text(sQueryMsg.replaceFirst("$2", msSelectedCategory)); if (xQueryDlg->run() == RET_NO) return; } if (SaveTemplate()) m_xDialog->response(RET_OK); else { OUString sText( SfxResId(STR_ERROR_SAVEAS) ); std::unique_ptr xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Warning, VclButtonsType::Ok, sText.replaceFirst("$1", msTemplateName))); xBox->run(); } } IMPL_LINK_NOARG(SfxSaveAsTemplateDialog, TemplateNameEditHdl, weld::Entry&, void) { msTemplateName = comphelper::string::strip(m_xTemplateNameEdit->get_text(), ' '); SelectCategoryHdl(*m_xLBCategory); } IMPL_LINK_NOARG(SfxSaveAsTemplateDialog, SelectCategoryHdl, weld::TreeView&, void) { if (m_xLBCategory->get_selected_index() == 0) { msSelectedCategory = OUString(); m_xOKButton->set_sensitive(false); } else { msSelectedCategory = m_xLBCategory->get_selected_text(); m_xOKButton->set_sensitive(!msTemplateName.isEmpty()); } } void SfxSaveAsTemplateDialog::initialize() { sal_uInt16 nCount = maDocTemplates.GetRegionCount(); for (sal_uInt16 i = 0; i < nCount; ++i) { OUString sCategoryName(maDocTemplates.GetFullRegionName(i)); msCategories.push_back(sCategoryName); } } void SfxSaveAsTemplateDialog::SetCategoryLBEntries(const std::vector& rFolderNames) { for (size_t i = 0, n = rFolderNames.size(); i < n; ++i) m_xLBCategory->insert_text(i+1, rFolderNames[i]); m_xLBCategory->select(0); } bool SfxSaveAsTemplateDialog::IsTemplateNameUnique() { std::vector::iterator it=find(msCategories.begin(), msCategories.end(), msSelectedCategory); mnRegionPos = std::distance(msCategories.begin(), it); sal_uInt16 nEntries = maDocTemplates.GetCount(mnRegionPos); for(sal_uInt16 i = 0; i < nEntries; i++) { OUString aName = maDocTemplates.GetName(mnRegionPos, i); if(aName == msTemplateName) return false; } return true; } bool SfxSaveAsTemplateDialog::SaveTemplate() { uno::Reference< frame::XStorable > xStorable(m_xModel, uno::UNO_QUERY_THROW ); uno::Reference< frame::XDocumentTemplates > xTemplates(frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) ); if (!xTemplates->storeTemplate( msSelectedCategory, msTemplateName, xStorable )) return false; sal_uInt16 nDocId = maDocTemplates.GetCount(mnRegionPos); OUString sURL = maDocTemplates.GetTemplateTargetURLFromComponent(msSelectedCategory, msTemplateName); bool bIsSaved = maDocTemplates.InsertTemplate( mnRegionPos, nDocId, msTemplateName, sURL); if (!bIsSaved) return false; if (!sURL.isEmpty() && m_xCBXDefault->get_active()) { OUString aServiceName; try { uno::Reference< embed::XStorage > xStorage = comphelper::OStorageHelper::GetStorageFromURL( sURL, embed::ElementModes::READ ); SotClipboardFormatId nFormat = SotStorage::GetFormatID( xStorage ); std::shared_ptr pFilter = SfxGetpApp()->GetFilterMatcher().GetFilter4ClipBoardId( nFormat ); if ( pFilter ) aServiceName = pFilter->GetServiceName(); } catch( uno::Exception& ) {} if(!aServiceName.isEmpty()) SfxObjectFactory::SetStandardTemplate(aServiceName, sURL); } maDocTemplates.Update(); return true; } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */