LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / templatedlg.cxx
blob0561c5f380dcb491bc5ee3f91c0342f69734343f
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 <sfx2/templatedlg.hxx>
12 #include <sfx2/inputdlg.hxx>
13 #include <sfx2/module.hxx>
15 #include <comphelper/processfactory.hxx>
16 #include <comphelper/propertyvalue.hxx>
17 #include <comphelper/string.hxx>
18 #include <comphelper/storagehelper.hxx>
19 #include <sfx2/app.hxx>
20 #include <sfx2/docfac.hxx>
21 #include <sfx2/docfilt.hxx>
22 #include <sfx2/fcontnr.hxx>
23 #include <sfx2/filedlghelper.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <sfx2/sfxresid.hxx>
26 #include <sfx2/templatedlglocalview.hxx>
27 #include <templatecontaineritem.hxx>
28 #include <templateviewitem.hxx>
29 #include <sfx2/thumbnailviewitem.hxx>
30 #include <sot/storage.hxx>
31 #include <svtools/imagemgr.hxx>
32 #include <tools/urlobj.hxx>
33 #include <unotools/moduleoptions.hxx>
34 #include <unotools/pathoptions.hxx>
35 #include <unotools/viewoptions.hxx>
36 #include <vcl/event.hxx>
37 #include <vcl/mnemonic.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/weld.hxx>
41 #include <com/sun/star/beans/NamedValue.hpp>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <com/sun/star/document/MacroExecMode.hpp>
44 #include <com/sun/star/document/UpdateDocMode.hpp>
45 #include <com/sun/star/embed/XStorage.hpp>
46 #include <com/sun/star/embed/ElementModes.hpp>
47 #include <com/sun/star/frame/Desktop.hpp>
48 #include <com/sun/star/frame/XStorable.hpp>
49 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
50 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
51 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
52 #include <com/sun/star/task/InteractionHandler.hpp>
53 #include <comphelper/dispatchcommand.hxx>
55 #include <sfx2/strings.hrc>
56 #include <bitmaps.hlst>
58 constexpr OUStringLiteral TM_SETTING_MANAGER = u"TemplateManager";
59 constexpr OUStringLiteral TM_SETTING_LASTFOLDER = u"LastFolder";
60 constexpr OUStringLiteral TM_SETTING_LASTAPPLICATION = u"LastApplication";
61 constexpr OUStringLiteral TM_SETTING_VIEWMODE = u"ViewMode";
63 #define MNI_ACTION_NEW_FOLDER "new"
64 #define MNI_ACTION_RENAME_FOLDER "rename"
65 #define MNI_ACTION_DELETE_FOLDER "delete"
66 #define MNI_ACTION_REFRESH "refresh"
67 #define MNI_ACTION_DEFAULT "default"
68 #define MNI_ACTION_DEFAULT_WRITER "default_writer"
69 #define MNI_ACTION_DEFAULT_CALC "default_calc"
70 #define MNI_ACTION_DEFAULT_IMPRESS "default_impress"
71 #define MNI_ACTION_DEFAULT_DRAW "default_draw"
72 #define MNI_ACTION_IMPORT "import_template"
73 #define MNI_ACTION_EXTENSIONS "extensions"
74 #define MNI_ALL_APPLICATIONS 0
75 #define MNI_WRITER 1
76 #define MNI_CALC 2
77 #define MNI_IMPRESS 3
78 #define MNI_DRAW 4
80 using namespace ::com::sun::star;
81 using namespace ::com::sun::star::beans;
82 using namespace ::com::sun::star::embed;
83 using namespace ::com::sun::star::frame;
84 using namespace ::com::sun::star::lang;
85 using namespace ::com::sun::star::uno;
86 using namespace ::com::sun::star::ui::dialogs;
87 using namespace ::com::sun::star::document;
89 static bool lcl_getServiceName (const OUString &rFileURL, OUString &rName );
91 static std::vector<OUString> lcl_getAllFactoryURLs ();
93 namespace {
95 class SearchView_Keyword
97 public:
99 SearchView_Keyword (const OUString &rKeyword, FILTER_APPLICATION App)
100 : maKeyword(rKeyword.toAsciiLowerCase()), meApp(App)
103 bool operator() (const TemplateItemProperties &rItem)
105 bool bRet = true;
107 INetURLObject aUrl(rItem.aPath);
108 OUString aExt = aUrl.getExtension();
110 if (meApp == FILTER_APPLICATION::WRITER)
112 bRet = aExt == "ott" || aExt == "stw" || aExt == "oth" || aExt == "dot" || aExt == "dotx";
114 else if (meApp == FILTER_APPLICATION::CALC)
116 bRet = aExt == "ots" || aExt == "stc" || aExt == "xlt" || aExt == "xltm" || aExt == "xltx";
118 else if (meApp == FILTER_APPLICATION::IMPRESS)
120 bRet = aExt == "otp" || aExt == "sti" || aExt == "pot" || aExt == "potm" || aExt == "potx";
122 else if (meApp == FILTER_APPLICATION::DRAW)
124 bRet = aExt == "otg" || aExt == "std";
127 return bRet && MatchSubstring(rItem.aName);
130 bool MatchSubstring( OUString const & sItemName )
132 if(maKeyword.isEmpty())
133 return false;
134 return sItemName.toAsciiLowerCase().indexOf(maKeyword) >= 0;
137 private:
139 OUString maKeyword;
140 FILTER_APPLICATION meApp;
145 /***
147 * Order items in ascending order (useful for the selection sets and move/copy operations since the associated ids
148 * change when processed by the SfxDocumentTemplates class so we want to process to ones with higher id first)
150 ***/
152 static bool cmpSelectionItems (const ThumbnailViewItem *pItem1, const ThumbnailViewItem *pItem2)
154 return pItem1->mnId > pItem2->mnId;
157 SfxTemplateManagerDlg::SfxTemplateManagerDlg(weld::Window *pParent)
158 : GenericDialogController(pParent, "sfx/ui/templatedlg.ui", "TemplateDialog")
159 , maSelTemplates(cmpSelectionItems)
160 , mxDesktop(Desktop::create(comphelper::getProcessComponentContext()))
161 , m_aUpdateDataTimer( "SfxTemplateManagerDlg UpdateDataTimer" )
162 , mxSearchFilter(m_xBuilder->weld_entry("search_filter"))
163 , mxCBApp(m_xBuilder->weld_combo_box("filter_application"))
164 , mxCBFolder(m_xBuilder->weld_combo_box("filter_folder"))
165 , mxOKButton(m_xBuilder->weld_button("ok"))
166 , mxCBXHideDlg(m_xBuilder->weld_check_button("hidedialogcb"))
167 , mxActionBar(m_xBuilder->weld_menu_button("action_menu"))
168 , mxLocalView(new TemplateDlgLocalView(m_xBuilder->weld_scrolled_window("scrolllocal", true),
169 m_xBuilder->weld_menu("contextmenu"),
170 m_xBuilder->weld_tree_view("tree_list")))
171 , mxLocalViewWeld(new weld::CustomWeld(*m_xBuilder, "template_view", *mxLocalView))
172 , mxListViewButton(m_xBuilder->weld_toggle_button("list_view_btn"))
173 , mxThumbnailViewButton(m_xBuilder->weld_toggle_button("thumbnail_view_btn"))
174 , mViewMode(TemplateViewMode::eThumbnailView)
176 // Create popup menus
177 mxActionBar->append_item(MNI_ACTION_NEW_FOLDER, SfxResId(STR_CATEGORY_NEW), BMP_ACTION_NEW_CATEGORY);
178 mxActionBar->append_item(MNI_ACTION_RENAME_FOLDER, SfxResId(STR_CATEGORY_RENAME), BMP_ACTION_RENAME);
179 mxActionBar->append_item(MNI_ACTION_DELETE_FOLDER, SfxResId(STR_CATEGORY_DELETE), BMP_ACTION_DELETE_CATEGORY);
180 mxActionBar->append_separator("separator");
181 mxActionBar->append_item(MNI_ACTION_REFRESH, SfxResId(STR_ACTION_REFRESH), BMP_ACTION_REFRESH);
182 mxActionBar->append_item(MNI_ACTION_DEFAULT, SfxResId(STR_ACTION_RESET_ALL_DEFAULT_TEMPLATES));
183 mxActionBar->append_item(MNI_ACTION_DEFAULT_WRITER, SfxResId(STR_ACTION_RESET_WRITER_TEMPLATE), BMP_ACTION_DEFAULT_WRITER);
184 mxActionBar->append_item(MNI_ACTION_DEFAULT_CALC, SfxResId(STR_ACTION_RESET_CALC_TEMPLATE), BMP_ACTION_DEFAULT_CALC);
185 mxActionBar->append_item(MNI_ACTION_DEFAULT_IMPRESS, SfxResId(STR_ACTION_RESET_IMPRESS_TEMPLATE), BMP_ACTION_DEFAULT_IMPRESS);
186 mxActionBar->append_item(MNI_ACTION_DEFAULT_DRAW, SfxResId(STR_ACTION_RESET_DRAW_TEMPLATE), BMP_ACTION_DEFAULT_DRAW);
187 mxActionBar->append_separator("separator2");
188 mxActionBar->append_item(MNI_ACTION_IMPORT, SfxResId(STR_ACTION_IMPORT), BMP_ACTION_IMPORT);
189 mxActionBar->append_item(MNI_ACTION_EXTENSIONS, SfxResId(STR_ACTION_EXTENSIONS), BMP_ACTION_EXTENSIONS);
191 mxActionBar->connect_selected(LINK(this,SfxTemplateManagerDlg,MenuSelectHdl));
193 mxLocalView->setItemMaxTextLength(TEMPLATE_ITEM_MAX_TEXT_LENGTH);
194 mxLocalView->setItemDimensions(TEMPLATE_ITEM_MAX_WIDTH,TEMPLATE_ITEM_THUMBNAIL_MAX_HEIGHT,
195 TEMPLATE_ITEM_MAX_HEIGHT-TEMPLATE_ITEM_THUMBNAIL_MAX_HEIGHT,
196 TEMPLATE_ITEM_PADDING);
198 mxLocalView->setItemStateHdl(LINK(this,SfxTemplateManagerDlg,TVItemStateHdl));
199 mxLocalView->setCreateContextMenuHdl(LINK(this,SfxTemplateManagerDlg, CreateContextMenuHdl));
200 mxLocalView->setOpenRegionHdl(LINK(this,SfxTemplateManagerDlg, OpenRegionHdl));
201 mxLocalView->setOpenTemplateHdl(LINK(this,SfxTemplateManagerDlg, OpenTemplateHdl));
202 mxLocalView->setEditTemplateHdl(LINK(this,SfxTemplateManagerDlg, EditTemplateHdl));
203 mxLocalView->setDeleteTemplateHdl(LINK(this,SfxTemplateManagerDlg, DeleteTemplateHdl));
204 mxLocalView->setDefaultTemplateHdl(LINK(this,SfxTemplateManagerDlg, DefaultTemplateHdl));
205 mxLocalView->setMoveTemplateHdl(LINK(this,SfxTemplateManagerDlg, MoveTemplateHdl));
206 mxLocalView->setExportTemplateHdl(LINK(this,SfxTemplateManagerDlg, ExportTemplateHdl));
208 mxLocalView->ShowTooltips(true);
210 mxOKButton->connect_clicked(LINK(this, SfxTemplateManagerDlg, OkClickHdl));
211 // FIXME: rather than disabling make dispatchCommand(".uno:AdditionsDialog") work in start center
212 if ( !SfxModule::GetActiveModule() )
213 mxActionBar->set_item_sensitive(MNI_ACTION_EXTENSIONS, false);
214 else
215 mxActionBar->set_item_sensitive(MNI_ACTION_EXTENSIONS, true);
216 mxListViewButton->connect_toggled(LINK(this, SfxTemplateManagerDlg, ListViewHdl));
217 mxThumbnailViewButton->connect_toggled(LINK(this, SfxTemplateManagerDlg, ThumbnailViewHdl));
219 mxSearchFilter->connect_changed(LINK(this, SfxTemplateManagerDlg, SearchUpdateHdl));
220 mxSearchFilter->connect_focus_in(LINK( this, SfxTemplateManagerDlg, GetFocusHdl ));
221 mxSearchFilter->connect_focus_out(LINK( this, SfxTemplateManagerDlg, LoseFocusHdl ));
222 mxSearchFilter->connect_key_press(LINK( this, SfxTemplateManagerDlg, KeyInputHdl));
224 mxActionBar->show();
226 mxLocalView->Populate();
227 mxLocalView->filterItems(ViewFilter_Application(FILTER_APPLICATION::NONE));
229 mxCBApp->set_active(0);
230 fillFolderComboBox();
232 mxActionBar->set_item_visible(MNI_ACTION_EXTENSIONS, true);
233 mxActionBar->set_item_visible(MNI_ACTION_IMPORT, true);
234 mxActionBar->set_item_visible(MNI_ACTION_NEW_FOLDER, true);
236 mxOKButton->set_label(SfxResId(STR_OPEN));
238 mxCBApp->connect_changed(LINK(this, SfxTemplateManagerDlg, SelectApplicationHdl));
239 mxCBFolder->connect_changed(LINK(this, SfxTemplateManagerDlg, SelectRegionHdl));
241 mxLocalView->Show();
243 m_aUpdateDataTimer.SetInvokeHandler(LINK(this, SfxTemplateManagerDlg, ImplUpdateDataHdl));
244 m_aUpdateDataTimer.SetTimeout(EDIT_UPDATEDATA_TIMEOUT);
246 mxLocalView->connect_focus_rect(LINK(this, SfxTemplateManagerDlg, FocusRectLocalHdl));
247 bMakeSelItemVisible = false;
250 SfxTemplateManagerDlg::~SfxTemplateManagerDlg()
252 writeSettings();
254 // Ignore view events since we are cleaning the object
255 mxLocalView->setItemStateHdl(Link<const ThumbnailViewItem*,void>());
256 mxLocalView->setOpenRegionHdl(Link<void*,void>());
257 mxLocalView->setOpenTemplateHdl(Link<ThumbnailViewItem*, void>());
260 short SfxTemplateManagerDlg::run()
262 //use application specific settings if there's no previous setting
263 getApplicationSpecificSettings();
264 readSettings();
265 updateMenuItems();
267 return weld::GenericDialogController::run();
270 IMPL_LINK(SfxTemplateManagerDlg, KeyInputHdl, const KeyEvent&, rKeyEvent, bool)
272 if (mxSearchFilter != nullptr && !mxSearchFilter->get_text().isEmpty())
274 vcl::KeyCode aKeyCode = rKeyEvent.GetKeyCode();
275 sal_uInt16 nKeyCode = aKeyCode.GetCode();
277 if ( nKeyCode == KEY_ESCAPE )
279 mxSearchFilter->set_text("");
280 SearchUpdateHdl(*mxSearchFilter);
281 return true;
284 return false;
287 void SfxTemplateManagerDlg::setDocumentModel(const uno::Reference<frame::XModel> &rModel)
289 m_xModel = rModel;
292 void SfxTemplateManagerDlg::setTemplateViewMode(TemplateViewMode eViewMode)
294 if(eViewMode == TemplateViewMode::eThumbnailView && mViewMode != TemplateViewMode::eThumbnailView)
296 mxThumbnailViewButton->set_state(TRISTATE_TRUE);
297 mxListViewButton->set_state(TRISTATE_FALSE);
298 mxLocalView->ThumbnailView::GrabFocus();
299 mViewMode = eViewMode;
300 mxLocalView->setTemplateViewMode(eViewMode);
301 mxLocalView->Show();
303 if(eViewMode == TemplateViewMode::eListView && mViewMode != TemplateViewMode::eListView)
305 mxListViewButton->set_state(TRISTATE_TRUE);
306 mxThumbnailViewButton->set_state(TRISTATE_FALSE);
307 mxLocalView->ListView::grab_focus();
308 mViewMode = eViewMode;
309 mxLocalView->setTemplateViewMode(eViewMode);
310 mxLocalView->Show();
314 TemplateViewMode SfxTemplateManagerDlg::getTemplateViewMode() const
316 return mViewMode;
320 FILTER_APPLICATION SfxTemplateManagerDlg::getCurrentApplicationFilter() const
322 const sal_Int16 nCurAppId = mxCBApp->get_active();
324 if (nCurAppId == MNI_WRITER)
325 return FILTER_APPLICATION::WRITER;
326 else if (nCurAppId == MNI_IMPRESS)
327 return FILTER_APPLICATION::IMPRESS;
328 else if (nCurAppId == MNI_CALC)
329 return FILTER_APPLICATION::CALC;
330 else if (nCurAppId == MNI_DRAW)
331 return FILTER_APPLICATION::DRAW;
333 return FILTER_APPLICATION::NONE;
336 void SfxTemplateManagerDlg::fillFolderComboBox()
338 std::vector<OUString> aFolderNames = mxLocalView->getFolderNames();
340 for (size_t i = 0, n = aFolderNames.size(); i < n; ++i)
341 mxCBFolder->append_text(aFolderNames[i]);
342 mxCBFolder->set_active(0);
343 mxActionBar->set_item_sensitive(MNI_ACTION_RENAME_FOLDER, false);
344 mxActionBar->set_item_sensitive(MNI_ACTION_DELETE_FOLDER, false);
347 void SfxTemplateManagerDlg::getApplicationSpecificSettings()
349 if ( ! m_xModel.is() )
351 mxCBApp->set_active(0);
352 mxCBFolder->set_active(0);
353 mxActionBar->set_item_sensitive(MNI_ACTION_RENAME_FOLDER, false);
354 mxActionBar->set_item_sensitive(MNI_ACTION_DELETE_FOLDER, false);
355 mxLocalView->filterItems(ViewFilter_Application(getCurrentApplicationFilter()));
356 mxLocalView->showAllTemplates();
357 return;
360 SvtModuleOptions::EFactory eFactory = SvtModuleOptions::ClassifyFactoryByModel(m_xModel);
362 switch(eFactory)
364 case SvtModuleOptions::EFactory::WRITER:
365 case SvtModuleOptions::EFactory::WRITERWEB:
366 case SvtModuleOptions::EFactory::WRITERGLOBAL:
367 mxCBApp->set_active(MNI_WRITER);
368 break;
369 case SvtModuleOptions::EFactory::CALC:
370 mxCBApp->set_active(MNI_CALC);
371 break;
372 case SvtModuleOptions::EFactory::IMPRESS:
373 mxCBApp->set_active(MNI_IMPRESS);
374 break;
375 case SvtModuleOptions::EFactory::DRAW:
376 mxCBApp->set_active(MNI_DRAW);
377 break;
378 default:
379 mxCBApp->set_active(0);
380 break;
383 mxLocalView->filterItems(ViewFilter_Application(getCurrentApplicationFilter()));
384 mxCBFolder->set_active(0);
385 mxActionBar->set_item_sensitive(MNI_ACTION_RENAME_FOLDER, false);
386 mxActionBar->set_item_sensitive(MNI_ACTION_DELETE_FOLDER, false);
387 mxLocalView->showAllTemplates();
390 void SfxTemplateManagerDlg::readSettings ()
392 OUString aLastFolder;
393 SvtViewOptions aViewSettings( EViewType::Dialog, TM_SETTING_MANAGER );
394 sal_Int16 nViewMode = -1;
396 if ( aViewSettings.Exists() )
398 sal_uInt16 nTmp = 0;
399 aViewSettings.GetUserItem(TM_SETTING_LASTFOLDER) >>= aLastFolder;
400 aViewSettings.GetUserItem(TM_SETTING_LASTAPPLICATION) >>= nTmp;
401 aViewSettings.GetUserItem(TM_SETTING_VIEWMODE) >>= nViewMode;
403 //open last remembered application only when application model is not set
404 if(!m_xModel.is())
406 switch (nTmp)
408 case MNI_WRITER:
409 mxCBApp->set_active(MNI_WRITER);
410 break;
411 case MNI_CALC:
412 mxCBApp->set_active(MNI_CALC);
413 break;
414 case MNI_IMPRESS:
415 mxCBApp->set_active(MNI_IMPRESS);
416 break;
417 case MNI_DRAW:
418 mxCBApp->set_active(MNI_DRAW);
419 break;
420 default:
421 mxCBApp->set_active(0);
422 break;
427 mxLocalView->filterItems(ViewFilter_Application(getCurrentApplicationFilter()));
429 if (aLastFolder.isEmpty())
431 //show all categories
432 mxCBFolder->set_active(0);
433 mxActionBar->set_item_sensitive(MNI_ACTION_RENAME_FOLDER, false);
434 mxActionBar->set_item_sensitive(MNI_ACTION_DELETE_FOLDER, false);
435 mxLocalView->showAllTemplates();
437 else
439 mxCBFolder->set_active_text(aLastFolder);
440 mxLocalView->showRegion(aLastFolder);
441 bool bIsBuiltInRegion = mxLocalView->IsBuiltInRegion(aLastFolder);
442 mxActionBar->set_item_sensitive(MNI_ACTION_RENAME_FOLDER, !bIsBuiltInRegion);
443 mxActionBar->set_item_sensitive(MNI_ACTION_DELETE_FOLDER, !bIsBuiltInRegion);
446 if(nViewMode == static_cast<sal_Int16>(TemplateViewMode::eListView) ||
447 nViewMode == static_cast<sal_Int16>(TemplateViewMode::eThumbnailView))
449 TemplateViewMode eViewMode = static_cast<TemplateViewMode>(nViewMode);
450 setTemplateViewMode(eViewMode);
452 else
454 //Default ViewMode
455 setTemplateViewMode(TemplateViewMode::eThumbnailView);
459 void SfxTemplateManagerDlg::writeSettings ()
461 OUString aLastFolder;
463 if (mxLocalView->getCurRegionId())
464 aLastFolder = mxLocalView->getRegionName(mxLocalView->getCurRegionId()-1);
466 // last folder
467 Sequence< NamedValue > aSettings
469 { TM_SETTING_LASTFOLDER, css::uno::makeAny(aLastFolder) },
470 { TM_SETTING_LASTAPPLICATION, css::uno::makeAny(sal_uInt16(mxCBApp->get_active())) },
471 { TM_SETTING_VIEWMODE, css::uno::makeAny(static_cast<sal_Int16>(getTemplateViewMode()))}
474 // write
475 SvtViewOptions aViewSettings(EViewType::Dialog, TM_SETTING_MANAGER);
476 aViewSettings.SetUserData(aSettings);
479 IMPL_LINK_NOARG(SfxTemplateManagerDlg, SelectApplicationHdl, weld::ComboBox&, void)
481 mxLocalView->filterItems(ViewFilter_Application(getCurrentApplicationFilter()));
482 SelectRegionHdl(*mxCBFolder);
483 updateMenuItems();
486 IMPL_LINK_NOARG(SfxTemplateManagerDlg, SelectRegionHdl, weld::ComboBox&, void)
488 const OUString sSelectedRegion = mxCBFolder->get_active_text();
490 if(mxCBFolder->get_active() == 0)
492 mxActionBar->set_item_sensitive(MNI_ACTION_RENAME_FOLDER, false);
493 mxActionBar->set_item_sensitive(MNI_ACTION_DELETE_FOLDER, false);
495 else
497 bool bIsBuiltInRegion = mxLocalView->IsBuiltInRegion(sSelectedRegion);
498 mxActionBar->set_item_sensitive(MNI_ACTION_RENAME_FOLDER, !bIsBuiltInRegion);
499 mxActionBar->set_item_sensitive(MNI_ACTION_DELETE_FOLDER, !bIsBuiltInRegion);
501 SearchUpdate();
504 IMPL_LINK(SfxTemplateManagerDlg, TVItemStateHdl, const ThumbnailViewItem*, pItem, void)
506 const TemplateViewItem *pViewItem = dynamic_cast<const TemplateViewItem*>(pItem);
508 if (pViewItem)
509 OnTemplateState(pItem);
512 IMPL_LINK(SfxTemplateManagerDlg, MenuSelectHdl, const OString&, rIdent, void)
514 if (rIdent == MNI_ACTION_NEW_FOLDER)
515 OnCategoryNew();
516 else if (rIdent == MNI_ACTION_RENAME_FOLDER)
517 OnCategoryRename();
518 else if (rIdent == MNI_ACTION_DELETE_FOLDER)
519 OnCategoryDelete();
520 else if (rIdent == MNI_ACTION_REFRESH)
522 mxLocalView->reload();
523 SearchUpdate();
525 else if (rIdent == MNI_ACTION_DEFAULT)
527 DefaultTemplateMenuSelectHdl(MNI_ACTION_DEFAULT_WRITER);
528 DefaultTemplateMenuSelectHdl(MNI_ACTION_DEFAULT_CALC);
529 DefaultTemplateMenuSelectHdl(MNI_ACTION_DEFAULT_IMPRESS);
530 DefaultTemplateMenuSelectHdl(MNI_ACTION_DEFAULT_DRAW);
532 else if(rIdent == MNI_ACTION_DEFAULT_WRITER || rIdent == MNI_ACTION_DEFAULT_CALC ||
533 rIdent == MNI_ACTION_DEFAULT_IMPRESS || rIdent == MNI_ACTION_DEFAULT_DRAW )
534 DefaultTemplateMenuSelectHdl(rIdent);
535 else if(rIdent == MNI_ACTION_IMPORT)
536 ImportActionHdl();
537 else if(rIdent == MNI_ACTION_EXTENSIONS)
538 ExtensionsActionHdl();
541 void SfxTemplateManagerDlg::DefaultTemplateMenuSelectHdl(std::string_view rIdent)
543 SvtModuleOptions aModOpt;
544 OUString aFactoryURL;
545 if (rIdent == MNI_ACTION_DEFAULT_WRITER)
546 aFactoryURL = aModOpt.GetFactoryEmptyDocumentURL( SvtModuleOptions::EFactory::WRITER);
547 else if (rIdent == MNI_ACTION_DEFAULT_CALC)
548 aFactoryURL = aModOpt.GetFactoryEmptyDocumentURL( SvtModuleOptions::EFactory::CALC);
549 else if (rIdent == MNI_ACTION_DEFAULT_IMPRESS)
550 aFactoryURL = aModOpt.GetFactoryEmptyDocumentURL( SvtModuleOptions::EFactory::IMPRESS);
551 else if (rIdent == MNI_ACTION_DEFAULT_DRAW)
552 aFactoryURL = aModOpt.GetFactoryEmptyDocumentURL( SvtModuleOptions::EFactory::DRAW);
553 else
554 return;
556 OUString aServiceName = SfxObjectShell::GetServiceNameFromFactory(aFactoryURL);
557 OUString sPrevDefault = SfxObjectFactory::GetStandardTemplate( aServiceName );
558 if(!sPrevDefault.isEmpty())
560 mxLocalView->RemoveDefaultTemplateIcon(sPrevDefault);
563 SfxObjectFactory::SetStandardTemplate( aServiceName, OUString() );
564 mxLocalView->refreshDefaultColumn();
565 updateMenuItems();
568 IMPL_LINK_NOARG(SfxTemplateManagerDlg, OkClickHdl, weld::Button&, void)
570 OnTemplateOpen();
571 m_xDialog->response(RET_OK);
574 IMPL_LINK_NOARG(SfxTemplateManagerDlg, MoveTemplateHdl, void*, void)
576 // modal dialog to select templates category
577 SfxTemplateCategoryDialog aDlg(m_xDialog.get());
578 aDlg.SetCategoryLBEntries(mxLocalView->getFolderNames());
580 size_t nItemId = 0;
582 if (aDlg.run() != RET_OK)
583 return;
585 const OUString& sCategory = aDlg.GetSelectedCategory();
586 bool bIsNewCategory = aDlg.IsNewCategoryCreated();
587 if(bIsNewCategory)
589 if (!sCategory.isEmpty())
591 nItemId = mxLocalView->createRegion(sCategory);
592 if(nItemId)
593 mxCBFolder->append_text(sCategory);
596 else
597 nItemId = mxLocalView->getRegionId(sCategory);
599 if(nItemId)
601 localMoveTo(nItemId);
604 mxLocalView->reload();
605 SearchUpdate();
607 IMPL_LINK_NOARG(SfxTemplateManagerDlg, ExportTemplateHdl, void*, void)
609 OnTemplateExport();
612 void SfxTemplateManagerDlg::ImportActionHdl()
614 if(mxCBFolder->get_active() == 0)
616 //Modal Dialog to select Category
617 SfxTemplateCategoryDialog aDlg(m_xDialog.get());
618 aDlg.SetCategoryLBEntries(mxLocalView->getFolderNames());
620 if (aDlg.run() == RET_OK)
622 const OUString& sCategory = aDlg.GetSelectedCategory();
623 bool bIsNewCategory = aDlg.IsNewCategoryCreated();
624 if(bIsNewCategory)
626 if(mxLocalView->createRegion(sCategory))
628 mxCBFolder->append_text(sCategory);
629 OnTemplateImportCategory(sCategory);
631 else
633 OUString aMsg( SfxResId(STR_CREATE_ERROR) );
634 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
635 VclMessageType::Warning, VclButtonsType::Ok,
636 aMsg.replaceFirst("$1", sCategory)));
637 xBox->run();
638 return;
641 else
642 OnTemplateImportCategory(sCategory);
645 else
647 const auto sCategory = mxCBFolder->get_active_text();
648 OnTemplateImportCategory(sCategory);
650 mxLocalView->reload();
651 SearchUpdate();
654 void SfxTemplateManagerDlg::ExtensionsActionHdl()
656 uno::Sequence<beans::PropertyValue> aArgs{ comphelper::makePropertyValue(
657 "AdditionsTag", OUString("Templates")) };
658 comphelper::dispatchCommand(".uno:AdditionsDialog", aArgs);
661 IMPL_LINK_NOARG(SfxTemplateManagerDlg, OpenRegionHdl, void*, void)
663 maSelTemplates.clear();
664 mxOKButton->set_sensitive(false);
665 mxActionBar->show();
668 IMPL_LINK(SfxTemplateManagerDlg, CreateContextMenuHdl, ThumbnailViewItem*, pItem, void)
670 const TemplateViewItem *pViewItem = dynamic_cast<TemplateViewItem*>(pItem);
671 bool bIsDefault = false;
672 bool bIsInternal = false;
673 std::vector<const TemplateViewItem*> aSelTemplates;
674 for(const auto& aSelTmpl : maSelTemplates)
676 const TemplateViewItem *aItem = dynamic_cast<const TemplateViewItem*>(aSelTmpl);
677 aSelTemplates.push_back(aItem);
680 for(const auto& aSelTmpl : aSelTemplates)
682 if(aSelTmpl->IsDefaultTemplate())
683 bIsDefault = true;
684 if(TemplateLocalView::IsInternalTemplate(aSelTmpl->getPath()))
686 bIsInternal = true;
687 if(bIsDefault)
688 break;
692 if (!pViewItem)
693 return;
695 bool bIsSingleSel = maSelTemplates.size() == 1;
696 OUString aDefaultImg;
697 INetURLObject aUrl(pViewItem->getPath());
698 if (ViewFilter_Application::isFilteredExtension(FILTER_APPLICATION::WRITER, aUrl.getExtension()))
699 aDefaultImg = BMP_ACTION_DEFAULT_WRITER;
700 else if (ViewFilter_Application::isFilteredExtension(FILTER_APPLICATION::CALC, aUrl.getExtension()))
701 aDefaultImg = BMP_ACTION_DEFAULT_CALC;
702 else if (ViewFilter_Application::isFilteredExtension(FILTER_APPLICATION::IMPRESS, aUrl.getExtension()))
703 aDefaultImg = BMP_ACTION_DEFAULT_IMPRESS;
704 else if (ViewFilter_Application::isFilteredExtension(FILTER_APPLICATION::DRAW, aUrl.getExtension()))
705 aDefaultImg = BMP_ACTION_DEFAULT_DRAW;
706 mxLocalView->createContextMenu(bIsDefault, bIsInternal, bIsSingleSel, aDefaultImg);
709 IMPL_LINK(SfxTemplateManagerDlg, OpenTemplateHdl, ThumbnailViewItem*, pItem, void)
711 uno::Sequence< PropertyValue > aArgs{
712 comphelper::makePropertyValue("AsTemplate", true),
713 comphelper::makePropertyValue("MacroExecutionMode", MacroExecMode::USE_CONFIG),
714 comphelper::makePropertyValue("UpdateDocMode", UpdateDocMode::ACCORDING_TO_CONFIG),
715 comphelper::makePropertyValue("InteractionHandler", task::InteractionHandler::createWithParent( ::comphelper::getProcessComponentContext(), nullptr )),
716 comphelper::makePropertyValue("ReadOnly", true)
719 TemplateViewItem *pTemplateItem = static_cast<TemplateViewItem*>(pItem);
723 mxDesktop->loadComponentFromURL(pTemplateItem->getPath(),"_default", 0, aArgs );
725 catch( const uno::Exception& )
729 m_xDialog->response(RET_OK);
732 IMPL_LINK(SfxTemplateManagerDlg, EditTemplateHdl, ThumbnailViewItem*, pItem, void)
734 uno::Sequence< PropertyValue > aArgs{
735 comphelper::makePropertyValue("AsTemplate", false),
736 comphelper::makePropertyValue("MacroExecutionMode", MacroExecMode::USE_CONFIG),
737 comphelper::makePropertyValue("UpdateDocMode", UpdateDocMode::ACCORDING_TO_CONFIG)
740 uno::Reference< XStorable > xStorable;
741 TemplateViewItem *pViewItem = static_cast<TemplateViewItem*>(pItem);
745 xStorable.set( mxDesktop->loadComponentFromURL(pViewItem->getPath(),"_default", 0, aArgs ),
746 uno::UNO_QUERY );
748 catch( const uno::Exception& )
752 m_xDialog->response(RET_OK);
755 IMPL_LINK_NOARG(SfxTemplateManagerDlg, DeleteTemplateHdl, void*, void)
757 std::set<const ThumbnailViewItem*,selection_cmp_fn> aSelTemplates = maSelTemplates;
758 OUString aDeletedTemplate;
760 for (auto const& pItem : aSelTemplates)
762 const TemplateViewItem *pViewItem = static_cast<const TemplateViewItem*>(pItem);
763 sal_uInt16 nRegionItemId = mxLocalView->getRegionId(pViewItem->mnRegionId);
765 if (!mxLocalView->removeTemplate(pViewItem->mnDocId + 1, nRegionItemId))//mnId w.r.t. region is mnDocId + 1;
767 aDeletedTemplate += pItem->maTitle+"\n";
771 if (!aDeletedTemplate.isEmpty())
773 OUString aMsg( SfxResId(STR_MSG_ERROR_DELETE_TEMPLATE) );
774 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
775 VclMessageType::Warning, VclButtonsType::Ok,
776 aMsg.replaceFirst("$1",aDeletedTemplate)));
777 xBox->run();
781 IMPL_LINK(SfxTemplateManagerDlg, DefaultTemplateHdl, ThumbnailViewItem*, pItem, void)
783 TemplateViewItem *pViewItem = static_cast<TemplateViewItem*>(pItem);
784 OUString aServiceName;
786 if(!pViewItem->IsDefaultTemplate())
788 if (lcl_getServiceName(pViewItem->getPath(),aServiceName))
790 OUString sPrevDefault = SfxObjectFactory::GetStandardTemplate( aServiceName );
791 if(!sPrevDefault.isEmpty())
793 mxLocalView->RemoveDefaultTemplateIcon(sPrevDefault);
795 SfxObjectFactory::SetStandardTemplate(aServiceName,pViewItem->getPath());
796 pViewItem->showDefaultIcon(true);
799 else
801 if(lcl_getServiceName(pViewItem->getPath(),aServiceName))
803 SfxObjectFactory::SetStandardTemplate( aServiceName, OUString() );
804 pViewItem->showDefaultIcon(false);
808 updateMenuItems();
811 IMPL_LINK_NOARG(SfxTemplateManagerDlg, SearchUpdateHdl, weld::Entry&, void)
813 m_aUpdateDataTimer.Start();
816 IMPL_LINK_NOARG(SfxTemplateManagerDlg, ImplUpdateDataHdl, Timer*, void)
818 SearchUpdate();
821 IMPL_LINK_NOARG(SfxTemplateManagerDlg, LoseFocusHdl, weld::Widget&, void)
823 if (m_aUpdateDataTimer.IsActive())
825 m_aUpdateDataTimer.Stop();
826 m_aUpdateDataTimer.Invoke();
830 IMPL_LINK_NOARG ( SfxTemplateManagerDlg, ListViewHdl, weld::Toggleable&, void )
832 setTemplateViewMode(TemplateViewMode::eListView);
835 IMPL_LINK_NOARG ( SfxTemplateManagerDlg, ThumbnailViewHdl, weld::Toggleable&, void )
837 setTemplateViewMode(TemplateViewMode::eThumbnailView);
838 bMakeSelItemVisible = true;
841 IMPL_LINK_NOARG(SfxTemplateManagerDlg, FocusRectLocalHdl, weld::Widget&, tools::Rectangle)
843 if(bMakeSelItemVisible && !maSelTemplates.empty())
844 mxLocalView->MakeItemVisible((*maSelTemplates.begin())->mnId);
845 bMakeSelItemVisible = false;
846 return tools::Rectangle();
849 void SfxTemplateManagerDlg::SearchUpdate()
851 const OUString sSelectedRegion = mxCBFolder->get_active_text();
852 mxLocalView->setCurRegionId(mxLocalView->getRegionId(sSelectedRegion));
853 OUString aKeyword = mxSearchFilter->get_text();
854 mxLocalView->Clear();
855 std::function<bool(const TemplateItemProperties &)> aFunc =
856 [&](const TemplateItemProperties &rItem)->bool
858 return aKeyword.isEmpty() || SearchView_Keyword(aKeyword, getCurrentApplicationFilter())(rItem);
861 std::vector<TemplateItemProperties> aItems = mxLocalView->getFilteredItems(aFunc);
862 mxLocalView->insertItems(aItems, mxCBFolder->get_active()!=0, true);
863 mxLocalView->Invalidate();
866 IMPL_LINK_NOARG(SfxTemplateManagerDlg, GetFocusHdl, weld::Widget&, void)
868 mxLocalView->deselectItems();
869 maSelTemplates.clear();
872 void SfxTemplateManagerDlg::OnTemplateState (const ThumbnailViewItem *pItem)
874 bool bInSelection = maSelTemplates.find(pItem) != maSelTemplates.end();
876 if (pItem->isSelected())
878 if (maSelTemplates.empty())
880 mxOKButton->set_sensitive(true);
882 else if (maSelTemplates.size() != 1 || !bInSelection)
884 mxOKButton->set_sensitive(false);
887 if (!bInSelection)
888 maSelTemplates.insert(pItem);
890 else
892 if (bInSelection)
894 maSelTemplates.erase(pItem);
896 if (maSelTemplates.empty())
898 mxOKButton->set_sensitive(false);
900 else if (maSelTemplates.size() == 1)
902 mxOKButton->set_sensitive(true);
909 void SfxTemplateManagerDlg::OnTemplateImportCategory(std::u16string_view sCategory)
911 sfx2::FileDialogHelper aFileDlg(css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
912 FileDialogFlags::MultiSelection, m_xDialog.get());
913 aFileDlg.SetContext(sfx2::FileDialogHelper::TemplateImport);
915 // add "All" filter
916 aFileDlg.AddFilter( SfxResId(STR_SFX_FILTERNAME_ALL),
917 FILEDIALOG_FILTER_ALL );
919 // add template filter
920 OUString sFilterExt;
921 OUString sFilterName( SfxResId( STR_TEMPLATE_FILTER ) );
923 // add filters of modules which are installed
924 SvtModuleOptions aModuleOpt;
925 if ( aModuleOpt.IsModuleInstalled( SvtModuleOptions::EModule::WRITER ) )
926 sFilterExt += "*.ott;*.stw;*.oth;*.dotx;*.dot";
928 if ( aModuleOpt.IsModuleInstalled( SvtModuleOptions::EModule::CALC ) )
930 if ( !sFilterExt.isEmpty() )
931 sFilterExt += ";";
933 sFilterExt += "*.ots;*.stc;*.xltx;*.xlt";
936 if ( aModuleOpt.IsModuleInstalled( SvtModuleOptions::EModule::IMPRESS ) )
938 if ( !sFilterExt.isEmpty() )
939 sFilterExt += ";";
941 sFilterExt += "*.otp;*.sti;*.pot;*.potx";
944 if ( aModuleOpt.IsModuleInstalled( SvtModuleOptions::EModule::DRAW ) )
946 if ( !sFilterExt.isEmpty() )
947 sFilterExt += ";";
949 sFilterExt += "*.otg;*.std";
952 if ( !sFilterExt.isEmpty() )
953 sFilterExt += ";";
955 sFilterExt += "*.vor";
957 sFilterName += " (" + sFilterExt + ")";
959 aFileDlg.AddFilter( sFilterName, sFilterExt );
960 aFileDlg.SetCurrentFilter( sFilterName );
962 ErrCode nCode = aFileDlg.Execute();
964 if ( nCode != ERRCODE_NONE )
965 return;
967 const css::uno::Sequence<OUString> aFiles = aFileDlg.GetSelectedFiles();
969 if (!aFiles.hasElements())
970 return;
972 //Import to the selected regions
973 TemplateContainerItem* pContItem = mxLocalView->getRegion(sCategory);
974 if(!pContItem)
975 return;
977 OUString aTemplateList;
979 for (const auto& rFile : aFiles)
981 if(!mxLocalView->copyFrom(pContItem, rFile))
983 if (aTemplateList.isEmpty())
984 aTemplateList = rFile;
985 else
986 aTemplateList += "\n" + rFile;
990 if (!aTemplateList.isEmpty())
992 OUString aMsg(SfxResId(STR_MSG_ERROR_IMPORT));
993 aMsg = aMsg.replaceFirst("$1",pContItem->maTitle);
994 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
995 VclMessageType::Warning, VclButtonsType::Ok,
996 aMsg.replaceFirst("$2",aTemplateList)));
997 xBox->run();
1001 void SfxTemplateManagerDlg::OnTemplateExport()
1003 uno::Reference<XComponentContext> xContext(comphelper::getProcessComponentContext());
1004 uno::Reference<XFolderPicker2> xFolderPicker = sfx2::createFolderPicker(xContext, m_xDialog.get());
1006 xFolderPicker->setDisplayDirectory(SvtPathOptions().GetWorkPath());
1008 sal_Int16 nResult = xFolderPicker->execute();
1009 sal_Int16 nCount = maSelTemplates.size();
1011 if( nResult != ExecutableDialogResults::OK )
1012 return;
1014 OUString aTemplateList;
1015 INetURLObject aPathObj(xFolderPicker->getDirectory());
1016 aPathObj.setFinalSlash();
1018 // export templates from the current view
1020 sal_uInt16 i = 1;
1021 auto aSelTemplates = maSelTemplates;
1022 for (auto const& selTemplate : aSelTemplates)
1024 const TemplateViewItem *pItem = static_cast<const TemplateViewItem*>(selTemplate);
1026 INetURLObject aItemPath(pItem->getPath());
1028 if ( 1 == i )
1029 aPathObj.Append(aItemPath.getName());
1030 else
1031 aPathObj.setName(aItemPath.getName());
1033 OUString aPath = aPathObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1035 if (!mxLocalView->exportTo(pItem->mnDocId + 1, //mnId w.r.t. region = mDocId + 1
1036 mxLocalView->getRegionId(pItem->mnRegionId), //pItem->mnRegionId does not store actual region Id
1037 aPath))
1039 if (aTemplateList.isEmpty())
1040 aTemplateList = pItem->maTitle;
1041 else
1042 aTemplateList += "\n" + pItem->maTitle;
1044 ++i;
1045 mxLocalView->deselectItems();
1048 if (!aTemplateList.isEmpty())
1050 OUString aText( SfxResId(STR_MSG_ERROR_EXPORT) );
1051 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
1052 VclMessageType::Warning, VclButtonsType::Ok,
1053 aText.replaceFirst("$1",aTemplateList)));
1054 xBox->run();
1056 else
1058 OUString sText( SfxResId(STR_MSG_EXPORT_SUCCESS) );
1059 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
1060 VclMessageType::Info, VclButtonsType::Ok,
1061 sText.replaceFirst("$1", OUString::number(nCount))));
1062 xBox->run();
1066 void SfxTemplateManagerDlg::OnTemplateOpen ()
1068 ThumbnailViewItem *pItem = const_cast<ThumbnailViewItem*>(*maSelTemplates.begin());
1070 OpenTemplateHdl(pItem);
1073 void SfxTemplateManagerDlg::OnCategoryNew()
1075 InputDialog dlg(m_xDialog.get(), SfxResId(STR_INPUT_NEW));
1076 dlg.set_title(SfxResId(STR_WINDOW_TITLE_RENAME_NEW_CATEGORY));
1077 int ret = dlg.run();
1079 if (!ret)
1080 return;
1082 OUString aName = dlg.GetEntryText();
1084 if(mxLocalView->createRegion(aName))
1085 mxCBFolder->append_text(aName);
1086 else
1088 OUString aMsg( SfxResId(STR_CREATE_ERROR) );
1089 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
1090 VclMessageType::Warning, VclButtonsType::Ok,
1091 aMsg.replaceFirst("$1", aName)));
1092 xBox->run();
1096 void SfxTemplateManagerDlg::OnCategoryRename()
1098 OUString sCategory = mxCBFolder->get_active_text();
1099 InputDialog dlg(m_xDialog.get(), SfxResId(STR_INPUT_NEW));
1100 dlg.set_title(SfxResId(STR_WINDOW_TITLE_RENAME_CATEGORY));
1101 dlg.SetEntryText(sCategory);
1102 int ret = dlg.run();
1104 if (!ret)
1105 return;
1107 OUString aName = dlg.GetEntryText();
1109 if(mxLocalView->renameRegion(sCategory, aName))
1111 sal_Int32 nPos = mxCBFolder->find_text(sCategory);
1112 mxCBFolder->remove(nPos);
1113 mxCBFolder->insert_text(nPos, aName);
1114 mxCBFolder->set_active(nPos);
1116 mxLocalView->reload();
1117 SearchUpdate();
1119 else
1121 OUString aMsg( SfxResId(STR_CREATE_ERROR) );
1122 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
1123 VclMessageType::Warning, VclButtonsType::Ok,
1124 aMsg.replaceFirst("$1", aName)));
1125 xBox->run();
1129 void SfxTemplateManagerDlg::OnCategoryDelete()
1131 const auto sCategory = mxCBFolder->get_active_text();
1132 std::unique_ptr<weld::MessageDialog> popupDlg(Application::CreateMessageDialog(m_xDialog.get(),
1133 VclMessageType::Question, VclButtonsType::YesNo,
1134 SfxResId(STR_QMSG_SEL_FOLDER_DELETE).replaceFirst("$1",sCategory)));
1135 if (popupDlg->run() != RET_YES)
1136 return;
1138 sal_Int16 nItemId = mxLocalView->getRegionId(sCategory);
1140 if (!mxLocalView->removeRegion(nItemId))
1142 OUString sMsg( SfxResId(STR_MSG_ERROR_DELETE_FOLDER) );
1143 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
1144 VclMessageType::Warning, VclButtonsType::Ok,
1145 sMsg.replaceFirst("$1",sCategory)));
1146 xBox->run();
1148 else
1150 mxCBFolder->remove_text(sCategory);
1153 mxLocalView->reload();
1154 mxLocalView->showAllTemplates();
1155 mxCBApp->set_active(0);
1156 mxCBFolder->set_active(0);
1157 SearchUpdate();
1158 mxActionBar->set_item_sensitive(MNI_ACTION_RENAME_FOLDER, false);
1159 mxActionBar->set_item_sensitive(MNI_ACTION_DELETE_FOLDER, false);
1160 updateMenuItems();
1163 void SfxTemplateManagerDlg::updateMenuItems ()
1166 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT, false);
1167 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT_WRITER, false);
1168 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT_CALC, false);
1169 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT_IMPRESS, false);
1170 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT_DRAW, false);
1171 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT, false);
1172 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT_WRITER, false);
1173 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT_CALC, false);
1174 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT_IMPRESS, false);
1175 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT_DRAW, false);
1177 SvtModuleOptions aModOpt;
1178 if( mxCBApp->get_active() == MNI_WRITER)
1180 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT_WRITER, true);
1181 if(!aModOpt.GetFactoryStandardTemplate( SvtModuleOptions::EFactory::WRITER).isEmpty())
1182 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT_WRITER, true);
1184 else if( mxCBApp->get_active() == MNI_CALC )
1186 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT_CALC, true);
1187 if(!aModOpt.GetFactoryStandardTemplate( SvtModuleOptions::EFactory::CALC).isEmpty())
1188 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT_CALC, true);
1190 else if(mxCBApp->get_active() == MNI_IMPRESS)
1192 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT_IMPRESS, true);
1193 if(!aModOpt.GetFactoryStandardTemplate( SvtModuleOptions::EFactory::IMPRESS).isEmpty())
1194 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT_IMPRESS, true);
1196 else if(mxCBApp->get_active() == MNI_DRAW)
1198 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT_DRAW, true);
1199 if(!aModOpt.GetFactoryStandardTemplate( SvtModuleOptions::EFactory::DRAW).isEmpty())
1200 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT_DRAW, true);
1202 else if(mxCBApp->get_active() == MNI_ALL_APPLICATIONS)
1204 mxActionBar->set_item_visible(MNI_ACTION_DEFAULT, true);
1205 if(!lcl_getAllFactoryURLs().empty())
1206 mxActionBar->set_item_sensitive(MNI_ACTION_DEFAULT, true);
1210 void SfxTemplateManagerDlg::localMoveTo(sal_uInt16 nItemId)
1212 if (nItemId)
1214 // Move templates to desired folder if for some reason move fails
1215 // try copying them.
1216 mxLocalView->moveTemplates(maSelTemplates,nItemId);
1220 static bool lcl_getServiceName ( const OUString &rFileURL, OUString &rName )
1222 bool bRet = false;
1224 if ( !rFileURL.isEmpty() )
1228 uno::Reference< embed::XStorage > xStorage =
1229 comphelper::OStorageHelper::GetStorageFromURL( rFileURL, embed::ElementModes::READ );
1231 SotClipboardFormatId nFormat = SotStorage::GetFormatID( xStorage );
1233 std::shared_ptr<const SfxFilter> pFilter = SfxGetpApp()->GetFilterMatcher().GetFilter4ClipBoardId( nFormat );
1235 if ( pFilter )
1237 rName = pFilter->GetServiceName();
1238 bRet = true;
1241 catch( uno::Exception& )
1245 return bRet;
1248 static std::vector<OUString> lcl_getAllFactoryURLs ()
1250 SvtModuleOptions aModOpt;
1251 std::vector<OUString> aList;
1252 const css::uno::Sequence<OUString> &aServiceNames = aModOpt.GetAllServiceNames();
1254 for( const auto& rServiceName : aServiceNames )
1256 if ( ! SfxObjectFactory::GetStandardTemplate( rServiceName ).isEmpty() )
1258 SvtModuleOptions::EFactory eFac = SvtModuleOptions::EFactory::WRITER;
1259 SvtModuleOptions::ClassifyFactoryByName( rServiceName, eFac );
1260 aList.push_back(aModOpt.GetFactoryEmptyDocumentURL(eFac));
1264 return aList;
1268 // Class SfxTemplateCategoryDialog --------------------------------------------------
1270 SfxTemplateCategoryDialog::SfxTemplateCategoryDialog(weld::Window* pParent)
1271 : GenericDialogController(pParent, "sfx/ui/templatecategorydlg.ui", "TemplatesCategoryDialog")
1272 , mbIsNewCategory(false)
1273 , mxLBCategory(m_xBuilder->weld_tree_view("categorylb"))
1274 , mxSelectLabel(m_xBuilder->weld_label("select_label"))
1275 , mxNewCategoryEdit(m_xBuilder->weld_entry("category_entry"))
1276 , mxCreateLabel(m_xBuilder->weld_label("create_label"))
1277 , mxOKButton(m_xBuilder->weld_button("ok"))
1279 mxLBCategory->append_text(SfxResId(STR_CATEGORY_NONE));
1280 mxNewCategoryEdit->connect_changed(LINK(this, SfxTemplateCategoryDialog, NewCategoryEditHdl));
1281 mxLBCategory->set_size_request(mxLBCategory->get_approximate_digit_width() * 32,
1282 mxLBCategory->get_height_rows(8));
1283 mxLBCategory->connect_changed(LINK(this, SfxTemplateCategoryDialog, SelectCategoryHdl));
1284 mxOKButton->set_sensitive(false);
1287 SfxTemplateCategoryDialog::~SfxTemplateCategoryDialog()
1291 IMPL_LINK_NOARG(SfxTemplateCategoryDialog, NewCategoryEditHdl, weld::Entry&, void)
1293 OUString sParam = comphelper::string::strip(mxNewCategoryEdit->get_text(), ' ');
1294 mxLBCategory->set_sensitive(sParam.isEmpty());
1295 if(!sParam.isEmpty())
1297 msSelectedCategory = sParam;
1298 mbIsNewCategory = true;
1299 mxOKButton->set_sensitive(true);
1301 else
1303 SelectCategoryHdl(*mxLBCategory);
1304 mbIsNewCategory = false;
1308 IMPL_LINK_NOARG(SfxTemplateCategoryDialog, SelectCategoryHdl, weld::TreeView&, void)
1310 if (mxLBCategory->get_selected_index() == 0)
1312 msSelectedCategory = OUString();
1313 mxOKButton->set_sensitive(false);
1314 mxNewCategoryEdit->set_sensitive(true);
1316 else
1318 msSelectedCategory = mxLBCategory->get_selected_text();
1319 mxNewCategoryEdit->set_sensitive(false);
1320 mxOKButton->set_sensitive(true);
1323 mbIsNewCategory = false;
1326 void SfxTemplateCategoryDialog::SetCategoryLBEntries(std::vector<OUString> aFolderNames)
1328 for (size_t i = 0, n = aFolderNames.size(); i < n; ++i)
1329 mxLBCategory->append_text(aFolderNames[i]);
1330 mxLBCategory->select(0);
1333 // SfxTemplateSelectionDialog -----------------------------------------------------------------
1335 SfxTemplateSelectionDlg::SfxTemplateSelectionDlg(weld::Window* pParent)
1336 : SfxTemplateManagerDlg(pParent)
1337 , maIdle("sfx2 SfxTemplateManagerDlg maIdle")
1339 mxCBApp->set_active(MNI_IMPRESS);
1340 mxCBFolder->set_active(0);
1341 m_xDialog->set_title(SfxResId(STR_TEMPLATE_SELECTION));
1343 if (mxLocalView->IsVisible())
1345 mxLocalView->filterItems(ViewFilter_Application(getCurrentApplicationFilter()));
1346 mxLocalView->showAllTemplates();
1349 mxCBApp->set_sensitive(false);
1350 mxActionBar->show();
1351 mxCBXHideDlg->show();
1352 mxCBXHideDlg->set_active(true);
1354 mxLocalView->setOpenTemplateHdl(LINK(this,SfxTemplateSelectionDlg, OpenTemplateHdl));
1355 mxOKButton->connect_clicked(LINK(this, SfxTemplateSelectionDlg, OkClickHdl));
1356 updateMenuItems();
1359 SfxTemplateSelectionDlg::~SfxTemplateSelectionDlg()
1361 maIdle.Stop();
1364 short SfxTemplateSelectionDlg::run()
1366 // tdf#124597 at startup this dialog is launched before its parent window
1367 // has taken its final size. The parent size request is processed during
1368 // the dialogs event loop so configure this dialog to center to
1369 // the parents pending geometry request
1370 m_xDialog->set_centered_on_parent(true);
1372 // tdf#125079 toggle off the size tracking at some future idle point
1373 maIdle.SetPriority(TaskPriority::LOWEST);
1374 maIdle.SetInvokeHandler(LINK(this,SfxTemplateSelectionDlg,TimeOut));
1375 maIdle.Start();
1376 setTemplateViewMode(TemplateViewMode::eThumbnailView);
1378 return weld::GenericDialogController::run();
1381 IMPL_LINK_NOARG(SfxTemplateSelectionDlg, TimeOut, Timer*, void)
1383 m_xDialog->set_centered_on_parent(false);
1386 IMPL_LINK(SfxTemplateSelectionDlg, OpenTemplateHdl, ThumbnailViewItem*, pItem, void)
1388 TemplateViewItem *pViewItem = static_cast<TemplateViewItem*>(pItem);
1389 msTemplatePath = pViewItem->getPath();
1391 m_xDialog->response(RET_OK);
1394 IMPL_LINK_NOARG(SfxTemplateSelectionDlg, OkClickHdl, weld::Button&, void)
1396 TemplateViewItem *pViewItem = static_cast<TemplateViewItem*>(const_cast<ThumbnailViewItem*>(*maSelTemplates.begin()));
1397 msTemplatePath = pViewItem->getPath();
1399 m_xDialog->response(RET_OK);
1402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */