bump product version to 5.0.4.1
[LibreOffice.git] / sfx2 / source / control / templatelocalview.cxx
blob2dbbad5174e30d4b5584b6e51f8e970d2eb15a48
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/templatelocalview.hxx>
12 #include <comphelper/processfactory.hxx>
13 #include <sfx2/doctempl.hxx>
14 #include <sfx2/templatecontaineritem.hxx>
15 #include <sfx2/templateviewitem.hxx>
16 #include <svl/inettype.hxx>
17 #include <tools/urlobj.hxx>
18 #include <unotools/ucbstreamhelper.hxx>
19 #include <vcl/builderfactory.hxx>
20 #include <vcl/pngread.hxx>
22 #include <com/sun/star/embed/ElementModes.hpp>
23 #include <com/sun/star/embed/XStorage.hpp>
24 #include <com/sun/star/frame/DocumentTemplates.hpp>
25 #include <com/sun/star/lang/XComponent.hpp>
26 #include <com/sun/star/frame/XDocumentTemplates.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::frame;
33 static void lcl_updateThumbnails (TemplateContainerItem *pItem);
35 TemplateLocalView::TemplateLocalView ( vcl::Window* pParent)
36 : TemplateAbstractView(pParent),
37 mpDocTemplates(new SfxDocumentTemplates)
41 VCL_BUILDER_FACTORY(TemplateLocalView)
43 TemplateLocalView::~TemplateLocalView()
45 disposeOnce();
48 void TemplateLocalView::dispose()
50 for (size_t i = 0; i < maRegions.size(); ++i)
51 delete maRegions[i];
53 maRegions.clear();
55 delete mpDocTemplates;
56 TemplateAbstractView::dispose();
59 void TemplateLocalView::Populate ()
61 for (size_t i = 0; i < maRegions.size(); ++i)
62 delete maRegions[i];
64 maRegions.clear();
66 sal_uInt16 nCount = mpDocTemplates->GetRegionCount();
67 for (sal_uInt16 i = 0; i < nCount; ++i)
69 OUString aRegionName(mpDocTemplates->GetFullRegionName(i));
71 TemplateContainerItem* pItem = new TemplateContainerItem( *this, i+1 );
72 pItem->mnRegionId = i;
73 pItem->maTitle = aRegionName;
74 pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
76 sal_uInt16 nEntries = mpDocTemplates->GetCount(i);
78 for (sal_uInt16 j = 0; j < nEntries; ++j)
80 OUString aName = mpDocTemplates->GetName(i,j);
81 OUString aURL = mpDocTemplates->GetPath(i,j);
83 TemplateItemProperties aProperties;
84 aProperties.aIsFolder = false; // Flat hierarchy for the local filesystem (no nested folders)
85 aProperties.nId = j+1;
86 aProperties.nDocId = j;
87 aProperties.nRegionId = i;
88 aProperties.aName = aName;
89 aProperties.aPath = aURL;
90 aProperties.aThumbnail = TemplateAbstractView::fetchThumbnail(aURL,
91 TEMPLATE_THUMBNAIL_MAX_WIDTH,
92 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
94 pItem->maTemplates.push_back(aProperties);
97 lcl_updateThumbnails(pItem);
99 maRegions.push_back(pItem);
103 void TemplateLocalView::reload ()
105 mpDocTemplates->Update();
107 Populate();
109 // Check if we are currently browsing a region or root folder
110 if (mnCurRegionId)
112 sal_uInt16 nRegionId = mnCurRegionId - 1; //Is offset by 1
114 for (size_t i = 0; i < maRegions.size(); ++i)
116 if (maRegions[i]->mnRegionId == nRegionId)
118 showRegion(maRegions[i]);
119 break;
123 else
124 showRootRegion();
127 void TemplateLocalView::showRootRegion()
129 mnHeaderHeight = 0;
130 mnCurRegionId = 0;
131 maCurRegionName.clear();
133 // Clone root region items so they dont get invalidated when we open another region
134 std::vector<ThumbnailViewItem*> items(maRegions.size());
135 for (int i = 0, n = maRegions.size(); i < n; ++i)
137 TemplateContainerItem *pCur = maRegions[i];
138 TemplateContainerItem *pItem = new TemplateContainerItem(*this, pCur->mnId);
139 pItem->mnRegionId = pCur->mnRegionId;
140 pItem->maTitle = pCur->maTitle;
141 pItem->maTemplates = pCur->maTemplates;
142 pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
144 items[i] = pItem;
147 maAllButton->Show(false);
148 maFTName->Show(false);
150 updateItems(items);
152 maOpenRegionHdl.Call(NULL);
155 void TemplateLocalView::showRegion(ThumbnailViewItem *pItem)
157 mnHeaderHeight = maAllButton->GetSizePixel().getHeight() + maAllButton->GetPosPixel().Y() * 2;
159 mnCurRegionId = static_cast<TemplateContainerItem*>(pItem)->mnRegionId+1;
160 maCurRegionName = pItem->maTitle;
161 maAllButton->Show(true);
162 maFTName->Show(true);
164 insertItems(reinterpret_cast<TemplateContainerItem*>(pItem)->maTemplates);
166 maOpenRegionHdl.Call(NULL);
169 void TemplateLocalView::showRegion(const OUString &rName)
171 for (int i = 0, n = maRegions.size(); i < n; ++i)
173 if (maRegions[i]->maTitle == rName)
175 maFTName->SetText(rName);
176 showRegion(maRegions[i]);
177 break;
182 sal_uInt16 TemplateLocalView::getCurRegionItemId() const
184 for (size_t i = 0; i < maRegions.size(); ++i)
186 if (maRegions[i]->mnRegionId == mnCurRegionId-1)
187 return maRegions[i]->mnId;
190 return 0;
193 sal_uInt16 TemplateLocalView::getRegionId(size_t pos) const
195 assert(pos < maRegions.size());
197 return maRegions[pos]->mnId;
200 OUString TemplateLocalView::getRegionName(const sal_uInt16 nRegionId) const
202 return mpDocTemplates->GetRegionName(nRegionId);
205 OUString TemplateLocalView::getRegionItemName(const sal_uInt16 nItemId) const
207 for (size_t i = 0; i < maRegions.size(); ++i)
209 if (maRegions[i]->mnId == nItemId)
210 return maRegions[i]->maTitle;
213 return OUString();
216 std::vector<OUString> TemplateLocalView::getFolderNames()
218 size_t n = maRegions.size();
219 std::vector<OUString> ret(n);
221 for (size_t i = 0; i < n; ++i)
222 ret[i] = maRegions[i]->maTitle;
224 return ret;
227 std::vector<TemplateItemProperties>
228 TemplateLocalView::getFilteredItems(const boost::function<bool (const TemplateItemProperties&) > &rFunc) const
230 std::vector<TemplateItemProperties> aItems;
232 if (mnCurRegionId)
234 TemplateContainerItem *pFolderItem = maRegions[mnCurRegionId-1];
236 for (size_t j = 0; j < pFolderItem->maTemplates.size(); ++j)
238 if (rFunc(pFolderItem->maTemplates[j]))
239 aItems.push_back(pFolderItem->maTemplates[j]);
242 else
244 for (size_t i = 0; i < maRegions.size(); ++i)
246 TemplateContainerItem *pFolderItem = maRegions[i];
248 for (size_t j = 0; j < pFolderItem->maTemplates.size(); ++j)
250 if (rFunc(pFolderItem->maTemplates[j]))
251 aItems.push_back(pFolderItem->maTemplates[j]);
256 return aItems;
259 sal_uInt16 TemplateLocalView::createRegion(const OUString &rName)
261 sal_uInt16 nRegionId = mpDocTemplates->GetRegionCount(); // Next regionId
262 sal_uInt16 nItemId = getNextItemId();
264 if (!mpDocTemplates->InsertDir(rName,nRegionId))
265 return 0;
267 OUString aRegionName = rName;
269 // Insert to the region cache list and to the thumbnail item list
270 TemplateContainerItem* pItem = new TemplateContainerItem( *this, nItemId );
271 pItem->mnRegionId = nRegionId;
272 pItem->maTitle = aRegionName;
273 pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
275 maRegions.push_back(pItem);
277 pItem = new TemplateContainerItem(*this, nItemId);
278 pItem->mnRegionId = nRegionId;
279 pItem->maTitle = aRegionName;
280 pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
282 AppendItem(pItem);
284 CalculateItemPositions();
285 Invalidate();
287 return pItem->mnId;
290 bool TemplateLocalView::isNestedRegionAllowed() const
292 return !mnCurRegionId;
295 bool TemplateLocalView::isImportAllowed() const
297 return mnCurRegionId;
300 bool TemplateLocalView::removeRegion(const sal_uInt16 nItemId)
302 sal_uInt16 nRegionId = USHRT_MAX;
304 // Remove from the region cache list
305 std::vector<TemplateContainerItem*>::iterator pRegionIt;
306 for ( pRegionIt = maRegions.begin(); pRegionIt != maRegions.end();)
308 if ( (*pRegionIt)->mnId == nItemId )
310 if (!mpDocTemplates->Delete((*pRegionIt)->mnRegionId,USHRT_MAX))
311 return false;
313 nRegionId = (*pRegionIt)->mnRegionId;
315 delete *pRegionIt;
316 pRegionIt = maRegions.erase(pRegionIt);
318 else
320 // Synchronize regions cache ids with SfxDocumentTemplates
321 if (nRegionId != USHRT_MAX && (*pRegionIt)->mnRegionId > nRegionId)
322 --(*pRegionIt)->mnRegionId;
324 ++pRegionIt;
328 if (nRegionId == USHRT_MAX)
329 return false;
331 // Synchronize view regions ids with SfxDocumentTemplates
332 std::vector<ThumbnailViewItem*>::iterator pViewIt = mItemList.begin();
333 for ( pViewIt = mItemList.begin(); pViewIt != mItemList.end(); ++pViewIt)
335 if (static_cast<TemplateContainerItem*>(*pViewIt)->mnRegionId > nRegionId)
336 --static_cast<TemplateContainerItem*>(*pViewIt)->mnRegionId;
339 RemoveItem(nItemId);
341 return true;
344 bool TemplateLocalView::removeTemplate (const sal_uInt16 nItemId, const sal_uInt16 nSrcItemId)
346 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
348 if (maRegions[i]->mnId == nSrcItemId)
350 TemplateContainerItem *pItem = static_cast<TemplateContainerItem*>(maRegions[i]);
351 std::vector<TemplateItemProperties>::iterator pIter;
352 for (pIter = pItem->maTemplates.begin(); pIter != pItem->maTemplates.end(); ++pIter)
354 if (pIter->nId == nItemId)
356 if (!mpDocTemplates->Delete(pItem->mnRegionId,pIter->nDocId))
357 return false;
359 pIter = pItem->maTemplates.erase(pIter);
361 if (maRegions[i]->mnRegionId == mnCurRegionId-1)
363 RemoveItem(nItemId);
364 Invalidate();
367 // Update Doc Idx for all templates that follow
368 for (; pIter != pItem->maTemplates.end(); ++pIter)
369 pIter->nDocId = pIter->nDocId - 1;
371 break;
375 lcl_updateThumbnails(pItem);
377 CalculateItemPositions();
379 break;
383 return true;
386 bool TemplateLocalView::moveTemplate (const ThumbnailViewItem *pItem, const sal_uInt16 nSrcItem,
387 const sal_uInt16 nTargetItem)
389 TemplateContainerItem *pTarget = NULL;
390 TemplateContainerItem *pSrc = NULL;
392 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
394 if (maRegions[i]->mnId == nTargetItem)
395 pTarget = static_cast<TemplateContainerItem*>(maRegions[i]);
396 else if (maRegions[i]->mnId == nSrcItem)
397 pSrc = static_cast<TemplateContainerItem*>(maRegions[i]);
400 if (pTarget && pSrc)
402 sal_uInt16 nSrcRegionId = pSrc->mnRegionId;
403 sal_uInt16 nTargetRegion = pTarget->mnRegionId;
404 sal_uInt16 nTargetIdx = mpDocTemplates->GetCount(nTargetRegion); // Next Idx
406 const TemplateViewItem *pViewItem = static_cast<const TemplateViewItem*>(pItem);
408 bool bCopy = !mpDocTemplates->Move(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId);
410 if (bCopy)
412 if (!mpDocTemplates->Copy(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId))
413 return false;
415 // move template to destination
417 TemplateItemProperties aTemplateItem;
418 aTemplateItem.nId = nTargetIdx + 1;
419 aTemplateItem.nDocId = nTargetIdx;
420 aTemplateItem.nRegionId = nTargetRegion;
421 aTemplateItem.aName = pViewItem->maTitle;
422 aTemplateItem.aPath = mpDocTemplates->GetPath(nTargetRegion,nTargetIdx);
423 aTemplateItem.aThumbnail = pViewItem->maPreview1;
425 pTarget->maTemplates.push_back(aTemplateItem);
427 if (!bCopy)
429 // remove template from region cached data
431 std::vector<TemplateItemProperties>::iterator aIter;
432 for (aIter = pSrc->maTemplates.begin(); aIter != pSrc->maTemplates.end();)
434 if (aIter->nDocId == pViewItem->mnDocId)
436 aIter = pSrc->maTemplates.erase(aIter);
438 else
440 // Keep region document id synchronized with SfxDocumentTemplates
441 if (aIter->nDocId > pViewItem->mnDocId)
442 --aIter->nDocId;
444 ++aIter;
448 // Keep view document id synchronized with SfxDocumentTemplates
449 std::vector<ThumbnailViewItem*>::iterator pItemIter = mItemList.begin();
450 for (; pItemIter != mItemList.end(); ++pItemIter)
452 if (static_cast<TemplateViewItem*>(*pItemIter)->mnDocId > pViewItem->mnDocId)
453 --static_cast<TemplateViewItem*>(*pItemIter)->mnDocId;
457 lcl_updateThumbnails(pSrc);
458 lcl_updateThumbnails(pTarget);
460 CalculateItemPositions();
461 Invalidate();
463 return true;
466 return false;
469 bool TemplateLocalView::moveTemplates(const std::set<const ThumbnailViewItem*, selection_cmp_fn> &rItems,
470 const sal_uInt16 nTargetItem)
472 assert(mnCurRegionId); // Only allowed in non root regions
474 bool ret = true;
476 sal_uInt16 nSrcRegionId = mnCurRegionId-1;
478 TemplateContainerItem *pTarget = NULL;
479 TemplateContainerItem *pSrc = NULL;
481 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
483 if (maRegions[i]->mnId == nTargetItem)
484 pTarget = static_cast<TemplateContainerItem*>(maRegions[i]);
485 else if (maRegions[i]->mnRegionId == nSrcRegionId)
486 pSrc = static_cast<TemplateContainerItem*>(maRegions[i]);
489 if (pTarget && pSrc)
491 bool refresh = false;
493 sal_uInt16 nTargetRegion = pTarget->mnRegionId;
494 sal_uInt16 nTargetIdx = mpDocTemplates->GetCount(nTargetRegion); // Next Idx
495 std::vector<sal_uInt16> aItemIds; // List of moved items ids (also prevents the invalidation of rItems iterators when we remove them as we go)
497 std::set<const ThumbnailViewItem*,selection_cmp_fn>::const_iterator aSelIter;
498 for ( aSelIter = rItems.begin(); aSelIter != rItems.end(); ++aSelIter, ++nTargetIdx )
500 const TemplateViewItem *pViewItem = static_cast<const TemplateViewItem*>(*aSelIter);
502 bool bCopy = !mpDocTemplates->Move(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId);
504 if (bCopy)
506 if (!mpDocTemplates->Copy(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId))
508 ret = false;
509 continue;
513 // move template to destination
515 TemplateItemProperties aTemplateItem;
516 aTemplateItem.nId = nTargetIdx + 1;
517 aTemplateItem.nDocId = nTargetIdx;
518 aTemplateItem.nRegionId = nTargetRegion;
519 aTemplateItem.aName = pViewItem->maTitle;
520 aTemplateItem.aPath = mpDocTemplates->GetPath(nTargetRegion,nTargetIdx);
521 aTemplateItem.aThumbnail = pViewItem->maPreview1;
523 pTarget->maTemplates.push_back(aTemplateItem);
525 if (!bCopy)
527 // remove template from region cached data
529 std::vector<TemplateItemProperties>::iterator pPropIter;
530 for (pPropIter = pSrc->maTemplates.begin(); pPropIter != pSrc->maTemplates.end();)
532 if (pPropIter->nDocId == pViewItem->mnDocId)
534 pPropIter = pSrc->maTemplates.erase(pPropIter);
535 aItemIds.push_back(pViewItem->mnId);
537 else
539 // Keep region document id synchronized with SfxDocumentTemplates
540 if (pPropIter->nDocId > pViewItem->mnDocId)
541 --pPropIter->nDocId;
543 ++pPropIter;
547 // Keep view document id synchronized with SfxDocumentTemplates
548 std::vector<ThumbnailViewItem*>::iterator pItemIter = mItemList.begin();
549 for (; pItemIter != mItemList.end(); ++pItemIter)
551 if (static_cast<TemplateViewItem*>(*pItemIter)->mnDocId > pViewItem->mnDocId)
552 --static_cast<TemplateViewItem*>(*pItemIter)->mnDocId;
556 refresh = true;
559 // Remove items from the current view
560 for (std::vector<sal_uInt16>::iterator it = aItemIds.begin(); it != aItemIds.end(); ++it)
561 RemoveItem(*it);
563 if (refresh)
565 lcl_updateThumbnails(pSrc);
566 lcl_updateThumbnails(pTarget);
568 CalculateItemPositions();
569 Invalidate();
572 else
573 ret = false;
575 return ret;
578 bool TemplateLocalView::copyFrom(const sal_uInt16 nRegionItemId, const BitmapEx &rThumbnail,
579 const OUString &rPath)
581 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
583 if (maRegions[i]->mnId == nRegionItemId)
585 sal_uInt16 nId = 0;
586 sal_uInt16 nDocId = 0;
588 TemplateContainerItem *pRegionItem =
589 static_cast<TemplateContainerItem*>(maRegions[i]);
591 if (!pRegionItem->maTemplates.empty())
593 nId = (pRegionItem->maTemplates.back()).nId+1;
594 nDocId = (pRegionItem->maTemplates.back()).nDocId+1;
597 OUString aPath(rPath);
598 sal_uInt16 nRegionId = maRegions[i]->mnRegionId;
600 if (mpDocTemplates->CopyFrom(nRegionId,nDocId,aPath))
602 TemplateItemProperties aTemplate;
603 aTemplate.nId = nId;
604 aTemplate.nDocId = nDocId;
605 aTemplate.nRegionId = nRegionId;
606 aTemplate.aName = aPath;
607 aTemplate.aThumbnail = rThumbnail;
608 aTemplate.aPath = mpDocTemplates->GetPath(nRegionId,nDocId);
610 TemplateContainerItem *pItem =
611 static_cast<TemplateContainerItem*>(maRegions[i]);
613 pItem->maTemplates.push_back(aTemplate);
615 lcl_updateThumbnails(pItem);
617 return true;
620 break;
624 return false;
627 bool TemplateLocalView::copyFrom(const OUString &rPath)
629 assert(mnCurRegionId);
631 TemplateContainerItem *pRegItem = maRegions[mnCurRegionId-1];
633 sal_uInt16 nId = getNextItemId();
634 sal_uInt16 nDocId = 0;
635 sal_uInt16 nRegionId = pRegItem->mnRegionId;
637 OUString aPath(rPath);
639 if (!pRegItem->maTemplates.empty())
640 nDocId = (pRegItem->maTemplates.back()).nDocId+1;
642 if (!mpDocTemplates->CopyFrom(nRegionId,nDocId,aPath))
643 return false;
645 TemplateItemProperties aTemplate;
646 aTemplate.aIsFolder = false;
647 aTemplate.nId = nId;
648 aTemplate.nDocId = nDocId;
649 aTemplate.nRegionId = nRegionId;
650 aTemplate.aName = aPath;
651 aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(rPath,
652 TEMPLATE_THUMBNAIL_MAX_WIDTH,
653 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
654 aTemplate.aPath = rPath;
656 pRegItem->maTemplates.push_back(aTemplate);
658 insertItem(aTemplate);
660 return true;
663 bool TemplateLocalView::copyFrom (TemplateContainerItem *pItem, const OUString &rPath)
665 sal_uInt16 nId = 1;
666 sal_uInt16 nDocId = 0;
667 sal_uInt16 nRegionId = pItem->mnRegionId;
668 OUString aPath(rPath);
670 if (!pItem->maTemplates.empty())
672 nId = (pItem->maTemplates.back()).nId+1;
673 nDocId = (pItem->maTemplates.back()).nDocId+1;
676 if (mpDocTemplates->CopyFrom(nRegionId,nDocId,aPath))
678 TemplateItemProperties aTemplate;
679 aTemplate.aIsFolder = false;
680 aTemplate.nId = nId;
681 aTemplate.nDocId = nDocId;
682 aTemplate.nRegionId = nRegionId;
683 aTemplate.aName = aPath;
684 aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(rPath,
685 TEMPLATE_THUMBNAIL_MAX_WIDTH,
686 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
687 aTemplate.aPath = rPath;
689 pItem->maTemplates.push_back(aTemplate);
691 lcl_updateThumbnails(pItem);
693 CalculateItemPositions();
695 return true;
698 return false;
701 bool TemplateLocalView::exportTo(const sal_uInt16 nItemId, const sal_uInt16 nRegionItemId, const OUString &rName)
703 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
705 if (maRegions[i]->mnId == nRegionItemId)
707 TemplateContainerItem *pRegItem = maRegions[i];
709 std::vector<TemplateItemProperties>::iterator aIter;
710 for (aIter = pRegItem->maTemplates.begin(); aIter != pRegItem->maTemplates.end(); ++aIter)
712 if (aIter->nId == nItemId)
714 if (!mpDocTemplates->CopyTo(pRegItem->mnRegionId,aIter->nDocId,rName))
715 return false;
717 return true;
721 break;
725 return false;
728 bool TemplateLocalView::saveTemplateAs (sal_uInt16 nItemId,
729 com::sun::star::uno::Reference<com::sun::star::frame::XModel> &rModel,
730 const OUString &rName)
733 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
735 if (maRegions[i]->mnId == nItemId)
737 uno::Reference< frame::XStorable > xStorable(rModel, uno::UNO_QUERY_THROW );
739 uno::Reference< frame::XDocumentTemplates > xTemplates(
740 frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) );
742 if (!xTemplates->storeTemplate(mpDocTemplates->GetRegionName(maRegions[i]->mnRegionId),rName, xStorable ))
743 return false;
745 sal_uInt16 nDocId = maRegions[i]->maTemplates.size();
747 OUString aURL = mpDocTemplates->GetTemplateTargetURLFromComponent(mpDocTemplates->GetRegionName(maRegions[i]->mnRegionId),rName);
749 if(!mpDocTemplates->InsertTemplate(maRegions[i]->mnRegionId,nDocId,rName,aURL))
750 return false;
753 TemplateItemProperties aTemplate;
754 aTemplate.aIsFolder = false;
755 aTemplate.nId = getNextItemId();
756 aTemplate.nDocId = nDocId;
757 aTemplate.nRegionId = maRegions[i]->mnRegionId;
758 aTemplate.aName = rName;
759 aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(aURL,
760 TEMPLATE_THUMBNAIL_MAX_WIDTH,
761 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
762 aTemplate.aPath = aURL;
764 maRegions[i]->maTemplates.push_back(aTemplate);
766 insertItem(aTemplate);
768 return true;
772 return false;
775 bool TemplateLocalView::saveTemplateAs(TemplateContainerItem *pDstItem,
776 com::sun::star::uno::Reference<com::sun::star::frame::XModel> &rModel,
777 const OUString &rName)
779 uno::Reference< frame::XStorable > xStorable(rModel, uno::UNO_QUERY_THROW );
781 uno::Reference< frame::XDocumentTemplates > xTemplates(
782 frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) );
784 if (!xTemplates->storeTemplate(mpDocTemplates->GetRegionName(pDstItem->mnRegionId),rName, xStorable ))
785 return false;
787 sal_uInt16 nDocId = pDstItem->maTemplates.size();
788 OUString aURL = mpDocTemplates->GetTemplateTargetURLFromComponent(mpDocTemplates->GetRegionName(pDstItem->mnRegionId),rName);
790 if(!mpDocTemplates->InsertTemplate(pDstItem->mnRegionId,nDocId,rName,aURL))
791 return false;
793 TemplateItemProperties aTemplate;
794 aTemplate.aIsFolder = false;
795 aTemplate.nId = pDstItem->maTemplates.empty() ? 1 : pDstItem->maTemplates.back().nId+1;
796 aTemplate.nDocId = nDocId;
797 aTemplate.nRegionId = pDstItem->mnRegionId;
798 aTemplate.aName = rName;
799 aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(aURL,
800 TEMPLATE_THUMBNAIL_MAX_WIDTH,
801 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
802 aTemplate.aPath = aURL;
804 pDstItem->maTemplates.push_back(aTemplate);
806 return true;
809 bool TemplateLocalView::isTemplateNameUnique(const sal_uInt16 nRegionItemId, const OUString &rName) const
811 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
813 if (maRegions[i]->mnId == nRegionItemId)
815 TemplateContainerItem *pRegItem = maRegions[i];
817 std::vector<TemplateItemProperties>::iterator aIter;
818 for (aIter = pRegItem->maTemplates.begin(); aIter != pRegItem->maTemplates.end(); ++aIter)
820 if (aIter->aName == rName)
821 return false;
824 break;
828 return true;
831 bool TemplateLocalView::renameItem(ThumbnailViewItem* pItem, const OUString& sNewTitle)
833 sal_uInt16 nRegionId = 0;
834 sal_uInt16 nDocId = USHRT_MAX;
835 TemplateViewItem* pDocItem = dynamic_cast<TemplateViewItem*>( pItem );
836 TemplateContainerItem* pContainerItem = dynamic_cast<TemplateContainerItem*>( pItem );
837 if ( pDocItem )
839 nRegionId = pDocItem->mnRegionId;
840 nDocId = pDocItem->mnDocId;
842 else if ( pContainerItem )
844 nRegionId = pContainerItem->mnRegionId;
846 return mpDocTemplates->SetName( sNewTitle, nRegionId, nDocId );
849 static void lcl_updateThumbnails (TemplateContainerItem *pItem)
851 pItem->maPreview1.Clear();
852 pItem->maPreview2.Clear();
853 pItem->maPreview3.Clear();
854 pItem->maPreview4.Clear();
856 // Update folder thumbnails
857 for (size_t i = 0, n = pItem->maTemplates.size(); i < n && pItem->HasMissingPreview(); ++i)
859 if ( pItem->maPreview1.IsEmpty( ) )
861 pItem->maPreview1 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail,
862 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
863 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
865 else if ( pItem->maPreview2.IsEmpty() )
867 pItem->maPreview2 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail,
868 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
869 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
871 else if ( pItem->maPreview3.IsEmpty() )
873 pItem->maPreview3 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail,
874 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
875 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
877 else if ( pItem->maPreview4.IsEmpty() )
879 pItem->maPreview4 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail,
880 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
881 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
886 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */