Bump version to 4.1-6
[LibreOffice.git] / sfx2 / source / control / templatelocalview.cxx
blob994c8c2cdcf549f5d7e13deea6fb6a4fa9113cef
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/pngread.hxx>
21 #include <com/sun/star/embed/ElementModes.hpp>
22 #include <com/sun/star/embed/XStorage.hpp>
23 #include <com/sun/star/frame/DocumentTemplates.hpp>
24 #include <com/sun/star/lang/XComponent.hpp>
25 #include <com/sun/star/frame/XDocumentTemplates.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::frame;
32 static void lcl_updateThumbnails (TemplateContainerItem *pItem);
34 TemplateLocalView::TemplateLocalView ( Window* pParent, const ResId& rResId, bool bDisableTransientChildren)
35 : TemplateAbstractView(pParent,rResId,bDisableTransientChildren),
36 mpDocTemplates(new SfxDocumentTemplates)
40 TemplateLocalView::~TemplateLocalView()
42 for (size_t i = 0; i < maRegions.size(); ++i)
43 delete maRegions[i];
45 maRegions.clear();
47 delete mpDocTemplates;
50 void TemplateLocalView::Populate ()
52 for (size_t i = 0; i < maRegions.size(); ++i)
53 delete maRegions[i];
55 maRegions.clear();
57 sal_uInt16 nCount = mpDocTemplates->GetRegionCount();
58 for (sal_uInt16 i = 0; i < nCount; ++i)
60 OUString aRegionName(mpDocTemplates->GetFullRegionName(i));
62 TemplateContainerItem* pItem = new TemplateContainerItem( *this );
63 pItem->mnId = i+1;
64 pItem->mnRegionId = i;
65 pItem->maTitle = aRegionName;
66 pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
68 sal_uInt16 nEntries = mpDocTemplates->GetCount(i);
70 for (sal_uInt16 j = 0; j < nEntries; ++j)
72 OUString aName = mpDocTemplates->GetName(i,j);
73 OUString aURL = mpDocTemplates->GetPath(i,j);
75 TemplateItemProperties aProperties;
76 aProperties.aIsFolder = false; // Flat hierarchy for the local filesystem (no nested folders)
77 aProperties.nId = j+1;
78 aProperties.nDocId = j;
79 aProperties.nRegionId = i;
80 aProperties.aName = aName;
81 aProperties.aPath = aURL;
82 aProperties.aThumbnail = TemplateAbstractView::fetchThumbnail(aURL,
83 TEMPLATE_THUMBNAIL_MAX_WIDTH,
84 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
86 pItem->maTemplates.push_back(aProperties);
89 lcl_updateThumbnails(pItem);
91 maRegions.push_back(pItem);
95 void TemplateLocalView::reload ()
97 mpDocTemplates->Update();
99 Populate();
101 // Check if we are currently browsing a region or root folder
102 if (mnCurRegionId)
104 sal_uInt16 nRegionId = mnCurRegionId - 1; //Is offset by 1
106 for (size_t i = 0; i < maRegions.size(); ++i)
108 if (maRegions[i]->mnRegionId == nRegionId)
110 showRegion(maRegions[i]);
111 break;
115 else
116 showRootRegion();
119 void TemplateLocalView::showRootRegion()
121 mnHeaderHeight = 0;
122 mnCurRegionId = 0;
123 maCurRegionName = OUString();
125 // Clone root region items so they dont get invalidated when we open another region
126 std::vector<ThumbnailViewItem*> items(maRegions.size());
127 for (int i = 0, n = maRegions.size(); i < n; ++i)
129 TemplateContainerItem *pCur = maRegions[i];
130 TemplateContainerItem *pItem = new TemplateContainerItem(*this);
131 pItem->mnId = pCur->mnId;
132 pItem->mnRegionId = pCur->mnRegionId;
133 pItem->maTitle = pCur->maTitle;
134 pItem->maTemplates = pCur->maTemplates;
135 pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
137 items[i] = pItem;
140 maAllButton.Show(false);
141 maFTName.Show(false);
143 updateItems(items);
145 maOpenRegionHdl.Call(NULL);
148 void TemplateLocalView::showRegion(ThumbnailViewItem *pItem)
150 mnHeaderHeight = maAllButton.GetSizePixel().getHeight() + maAllButton.GetPosPixel().Y() * 2;
152 mnCurRegionId = static_cast<TemplateContainerItem*>(pItem)->mnRegionId+1;
153 maCurRegionName = pItem->maTitle;
154 maAllButton.Show(true);
155 maFTName.Show(true);
157 insertItems(reinterpret_cast<TemplateContainerItem*>(pItem)->maTemplates);
159 maOpenRegionHdl.Call(NULL);
162 void TemplateLocalView::showRegion(const OUString &rName)
164 for (int i = 0, n = maRegions.size(); i < n; ++i)
166 if (maRegions[i]->maTitle == rName)
168 maFTName.SetText(rName);
169 showRegion(maRegions[i]);
170 break;
175 sal_uInt16 TemplateLocalView::getCurRegionItemId() const
177 for (size_t i = 0; i < maRegions.size(); ++i)
179 if (maRegions[i]->mnRegionId == mnCurRegionId-1)
180 return maRegions[i]->mnId;
183 return 0;
186 sal_uInt16 TemplateLocalView::getRegionId(size_t pos) const
188 assert(pos < maRegions.size());
190 return maRegions[pos]->mnId;
193 OUString TemplateLocalView::getRegionName(const sal_uInt16 nRegionId) const
195 return mpDocTemplates->GetRegionName(nRegionId);
198 OUString TemplateLocalView::getRegionItemName(const sal_uInt16 nItemId) const
200 for (size_t i = 0; i < maRegions.size(); ++i)
202 if (maRegions[i]->mnId == nItemId)
203 return maRegions[i]->maTitle;
206 return OUString();
209 std::vector<OUString> TemplateLocalView::getFolderNames()
211 size_t n = maRegions.size();
212 std::vector<OUString> ret(n);
214 for (size_t i = 0; i < n; ++i)
215 ret[i] = maRegions[i]->maTitle;
217 return ret;
220 std::vector<TemplateItemProperties>
221 TemplateLocalView::getFilteredItems(const boost::function<bool (const TemplateItemProperties&) > &rFunc) const
223 std::vector<TemplateItemProperties> aItems;
225 if (mnCurRegionId)
227 TemplateContainerItem *pFolderItem = maRegions[mnCurRegionId-1];
229 for (size_t j = 0; j < pFolderItem->maTemplates.size(); ++j)
231 if (rFunc(pFolderItem->maTemplates[j]))
232 aItems.push_back(pFolderItem->maTemplates[j]);
235 else
237 for (size_t i = 0; i < maRegions.size(); ++i)
239 TemplateContainerItem *pFolderItem = maRegions[i];
241 for (size_t j = 0; j < pFolderItem->maTemplates.size(); ++j)
243 if (rFunc(pFolderItem->maTemplates[j]))
244 aItems.push_back(pFolderItem->maTemplates[j]);
249 return aItems;
252 sal_uInt16 TemplateLocalView::createRegion(const OUString &rName)
254 sal_uInt16 nRegionId = mpDocTemplates->GetRegionCount(); // Next regionId
255 sal_uInt16 nItemId = getNextItemId();
257 if (!mpDocTemplates->InsertDir(rName,nRegionId))
258 return false;
260 OUString aRegionName = rName;
262 // Insert to the region cache list and to the thumbnail item list
263 TemplateContainerItem* pItem = new TemplateContainerItem( *this );
264 pItem->mnId = nItemId;
265 pItem->mnRegionId = nRegionId;
266 pItem->maTitle = aRegionName;
267 pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
269 maRegions.push_back(pItem);
271 pItem = new TemplateContainerItem(*this);
272 pItem->mnId = nItemId;
273 pItem->mnRegionId = nRegionId;
274 pItem->maTitle = aRegionName;
275 pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
277 AppendItem(pItem);
279 CalculateItemPositions();
280 Invalidate();
282 return pItem->mnId;
285 bool TemplateLocalView::isNestedRegionAllowed() const
287 return !mnCurRegionId;
290 bool TemplateLocalView::isImportAllowed() const
292 return mnCurRegionId;
295 bool TemplateLocalView::removeRegion(const sal_uInt16 nItemId)
297 sal_uInt16 nRegionId = USHRT_MAX;
299 // Remove from the region cache list
300 std::vector<TemplateContainerItem*>::iterator pRegionIt;
301 for ( pRegionIt = maRegions.begin(); pRegionIt != maRegions.end();)
303 if ( (*pRegionIt)->mnId == nItemId )
305 if (!mpDocTemplates->Delete((*pRegionIt)->mnRegionId,USHRT_MAX))
306 return false;
308 nRegionId = (*pRegionIt)->mnRegionId;
310 delete *pRegionIt;
311 pRegionIt = maRegions.erase(pRegionIt);
313 else
315 // Syncronize regions cache ids with SfxDocumentTemplates
316 if (nRegionId != USHRT_MAX && (*pRegionIt)->mnRegionId > nRegionId)
317 --(*pRegionIt)->mnRegionId;
319 ++pRegionIt;
323 if (nRegionId == USHRT_MAX)
324 return false;
326 // Syncronize view regions ids with SfxDocumentTemplates
327 std::vector<ThumbnailViewItem*>::iterator pViewIt = mItemList.begin();
328 for ( pViewIt = mItemList.begin(); pViewIt != mItemList.end(); ++pViewIt)
330 if (static_cast<TemplateContainerItem*>(*pViewIt)->mnRegionId > nRegionId)
331 --static_cast<TemplateContainerItem*>(*pViewIt)->mnRegionId;
334 RemoveItem(nItemId);
336 return true;
339 bool TemplateLocalView::removeTemplate (const sal_uInt16 nItemId, const sal_uInt16 nSrcItemId)
341 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
343 if (maRegions[i]->mnId == nSrcItemId)
345 TemplateContainerItem *pItem = static_cast<TemplateContainerItem*>(maRegions[i]);
346 std::vector<TemplateItemProperties>::iterator pIter;
347 for (pIter = pItem->maTemplates.begin(); pIter != pItem->maTemplates.end(); ++pIter)
349 if (pIter->nId == nItemId)
351 if (!mpDocTemplates->Delete(pItem->mnRegionId,pIter->nDocId))
352 return false;
354 pIter = pItem->maTemplates.erase(pIter);
356 if (maRegions[i]->mnRegionId == mnCurRegionId-1)
358 RemoveItem(nItemId);
359 Invalidate();
362 // Update Doc Idx for all templates that follow
363 for (; pIter != pItem->maTemplates.end(); ++pIter)
364 pIter->nDocId = pIter->nDocId - 1;
366 break;
370 lcl_updateThumbnails(pItem);
372 CalculateItemPositions();
374 break;
378 return true;
381 bool TemplateLocalView::moveTemplate (const ThumbnailViewItem *pItem, const sal_uInt16 nSrcItem,
382 const sal_uInt16 nTargetItem)
384 TemplateContainerItem *pTarget = NULL;
385 TemplateContainerItem *pSrc = NULL;
387 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
389 if (maRegions[i]->mnId == nTargetItem)
390 pTarget = static_cast<TemplateContainerItem*>(maRegions[i]);
391 else if (maRegions[i]->mnId == nSrcItem)
392 pSrc = static_cast<TemplateContainerItem*>(maRegions[i]);
395 if (pTarget && pSrc)
397 sal_uInt16 nSrcRegionId = pSrc->mnRegionId;
398 sal_uInt16 nTargetRegion = pTarget->mnRegionId;
399 sal_uInt16 nTargetIdx = mpDocTemplates->GetCount(nTargetRegion); // Next Idx
401 const TemplateViewItem *pViewItem = static_cast<const TemplateViewItem*>(pItem);
403 bool bCopy = !mpDocTemplates->Move(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId);
405 if (bCopy)
407 if (!mpDocTemplates->Copy(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId))
408 return false;
410 // move template to destination
412 TemplateItemProperties aTemplateItem;
413 aTemplateItem.nId = nTargetIdx + 1;
414 aTemplateItem.nDocId = nTargetIdx;
415 aTemplateItem.nRegionId = nTargetRegion;
416 aTemplateItem.aName = pViewItem->maTitle;
417 aTemplateItem.aPath = mpDocTemplates->GetPath(nTargetRegion,nTargetIdx);
418 aTemplateItem.aThumbnail = pViewItem->maPreview1;
420 pTarget->maTemplates.push_back(aTemplateItem);
422 if (!bCopy)
424 // remove template from region cached data
426 std::vector<TemplateItemProperties>::iterator aIter;
427 for (aIter = pSrc->maTemplates.begin(); aIter != pSrc->maTemplates.end();)
429 if (aIter->nDocId == pViewItem->mnDocId)
431 aIter = pSrc->maTemplates.erase(aIter);
433 else
435 // Keep region document id syncronized with SfxDocumentTemplates
436 if (aIter->nDocId > pViewItem->mnDocId)
437 --aIter->nDocId;
439 ++aIter;
443 // Keep view document id syncronized with SfxDocumentTemplates
444 std::vector<ThumbnailViewItem*>::iterator pItemIter = mItemList.begin();
445 for (; pItemIter != mItemList.end(); ++pItemIter)
447 if (static_cast<TemplateViewItem*>(*pItemIter)->mnDocId > pViewItem->mnDocId)
448 --static_cast<TemplateViewItem*>(*pItemIter)->mnDocId;
452 lcl_updateThumbnails(pSrc);
453 lcl_updateThumbnails(pTarget);
455 CalculateItemPositions();
456 Invalidate();
458 return true;
461 return false;
464 bool TemplateLocalView::moveTemplates(const std::set<const ThumbnailViewItem*, selection_cmp_fn> &rItems,
465 const sal_uInt16 nTargetItem)
467 assert(mnCurRegionId); // Only allowed in non root regions
469 bool ret = true;
470 bool refresh = false;
472 sal_uInt16 nSrcRegionId = mnCurRegionId-1;
474 TemplateContainerItem *pTarget = NULL;
475 TemplateContainerItem *pSrc = NULL;
477 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
479 if (maRegions[i]->mnId == nTargetItem)
480 pTarget = static_cast<TemplateContainerItem*>(maRegions[i]);
481 else if (maRegions[i]->mnRegionId == nSrcRegionId)
482 pSrc = static_cast<TemplateContainerItem*>(maRegions[i]);
485 if (pTarget && pSrc)
487 sal_uInt16 nTargetRegion = pTarget->mnRegionId;
488 sal_uInt16 nTargetIdx = mpDocTemplates->GetCount(nTargetRegion); // Next Idx
489 std::vector<sal_uInt16> aItemIds; // List of moved items ids (also prevents the invalidation of rItems iterators when we remove them as we go)
491 std::set<const ThumbnailViewItem*,selection_cmp_fn>::const_iterator aSelIter;
492 for ( aSelIter = rItems.begin(); aSelIter != rItems.end(); ++aSelIter, ++nTargetIdx )
494 const TemplateViewItem *pViewItem = static_cast<const TemplateViewItem*>(*aSelIter);
496 bool bCopy = !mpDocTemplates->Move(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId);
498 if (bCopy)
500 if (!mpDocTemplates->Copy(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId))
502 ret = false;
503 continue;
507 // move template to destination
509 TemplateItemProperties aTemplateItem;
510 aTemplateItem.nId = nTargetIdx + 1;
511 aTemplateItem.nDocId = nTargetIdx;
512 aTemplateItem.nRegionId = nTargetRegion;
513 aTemplateItem.aName = pViewItem->maTitle;
514 aTemplateItem.aPath = mpDocTemplates->GetPath(nTargetRegion,nTargetIdx);
515 aTemplateItem.aThumbnail = pViewItem->maPreview1;
517 pTarget->maTemplates.push_back(aTemplateItem);
519 if (!bCopy)
521 // remove template from region cached data
523 std::vector<TemplateItemProperties>::iterator pPropIter;
524 for (pPropIter = pSrc->maTemplates.begin(); pPropIter != pSrc->maTemplates.end();)
526 if (pPropIter->nDocId == pViewItem->mnDocId)
528 pPropIter = pSrc->maTemplates.erase(pPropIter);
529 aItemIds.push_back(pViewItem->mnId);
531 else
533 // Keep region document id syncronized with SfxDocumentTemplates
534 if (pPropIter->nDocId > pViewItem->mnDocId)
535 --pPropIter->nDocId;
537 ++pPropIter;
541 // Keep view document id syncronized with SfxDocumentTemplates
542 std::vector<ThumbnailViewItem*>::iterator pItemIter = mItemList.begin();
543 for (; pItemIter != mItemList.end(); ++pItemIter)
545 if (static_cast<TemplateViewItem*>(*pItemIter)->mnDocId > pViewItem->mnDocId)
546 --static_cast<TemplateViewItem*>(*pItemIter)->mnDocId;
550 refresh = true;
553 // Remove items from the current view
554 for (std::vector<sal_uInt16>::iterator it = aItemIds.begin(); it != aItemIds.end(); ++it)
555 RemoveItem(*it);
557 if (refresh)
559 lcl_updateThumbnails(pSrc);
560 lcl_updateThumbnails(pTarget);
562 CalculateItemPositions();
563 Invalidate();
566 else
567 ret = false;
569 return ret;
572 bool TemplateLocalView::copyFrom(const sal_uInt16 nRegionItemId, const BitmapEx &rThumbnail,
573 const OUString &rPath)
575 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
577 if (maRegions[i]->mnId == nRegionItemId)
579 sal_uInt16 nId = 0;
580 sal_uInt16 nDocId = 0;
582 TemplateContainerItem *pRegionItem =
583 static_cast<TemplateContainerItem*>(maRegions[i]);
585 if (!pRegionItem->maTemplates.empty())
587 nId = (pRegionItem->maTemplates.back()).nId+1;
588 nDocId = (pRegionItem->maTemplates.back()).nDocId+1;
591 String aPath(rPath);
592 sal_uInt16 nRegionId = maRegions[i]->mnRegionId;
594 if (mpDocTemplates->CopyFrom(nRegionId,nDocId,aPath))
596 TemplateItemProperties aTemplate;
597 aTemplate.nId = nId;
598 aTemplate.nDocId = nDocId;
599 aTemplate.nRegionId = nRegionId;
600 aTemplate.aName = aPath;
601 aTemplate.aThumbnail = rThumbnail;
602 aTemplate.aPath = mpDocTemplates->GetPath(nRegionId,nDocId);
604 TemplateContainerItem *pItem =
605 static_cast<TemplateContainerItem*>(maRegions[i]);
607 pItem->maTemplates.push_back(aTemplate);
609 lcl_updateThumbnails(pItem);
611 return true;
614 break;
618 return false;
621 bool TemplateLocalView::copyFrom(const OUString &rPath)
623 assert(mnCurRegionId);
625 TemplateContainerItem *pRegItem = maRegions[mnCurRegionId-1];
627 sal_uInt16 nId = getNextItemId();
628 sal_uInt16 nDocId = 0;
629 sal_uInt16 nRegionId = pRegItem->mnRegionId;
631 String aPath(rPath);
633 if (!pRegItem->maTemplates.empty())
634 nDocId = (pRegItem->maTemplates.back()).nDocId+1;
636 if (!mpDocTemplates->CopyFrom(nRegionId,nDocId,aPath))
637 return false;
639 TemplateItemProperties aTemplate;
640 aTemplate.aIsFolder = false;
641 aTemplate.nId = nId;
642 aTemplate.nDocId = nDocId;
643 aTemplate.nRegionId = nRegionId;
644 aTemplate.aName = aPath;
645 aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(rPath,
646 TEMPLATE_THUMBNAIL_MAX_WIDTH,
647 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
648 aTemplate.aPath = rPath;
650 pRegItem->maTemplates.push_back(aTemplate);
652 insertItem(aTemplate);
654 return true;
657 bool TemplateLocalView::copyFrom (TemplateContainerItem *pItem, const OUString &rPath)
659 sal_uInt16 nId = 1;
660 sal_uInt16 nDocId = 0;
661 sal_uInt16 nRegionId = pItem->mnRegionId;
662 String aPath(rPath);
664 if (!pItem->maTemplates.empty())
666 nId = (pItem->maTemplates.back()).nId+1;
667 nDocId = (pItem->maTemplates.back()).nDocId+1;
670 if (mpDocTemplates->CopyFrom(nRegionId,nDocId,aPath))
672 TemplateItemProperties aTemplate;
673 aTemplate.aIsFolder = false;
674 aTemplate.nId = nId;
675 aTemplate.nDocId = nDocId;
676 aTemplate.nRegionId = nRegionId;
677 aTemplate.aName = aPath;
678 aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(rPath,
679 TEMPLATE_THUMBNAIL_MAX_WIDTH,
680 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
681 aTemplate.aPath = rPath;
683 pItem->maTemplates.push_back(aTemplate);
685 lcl_updateThumbnails(pItem);
687 CalculateItemPositions();
689 return true;
692 return false;
695 bool TemplateLocalView::exportTo(const sal_uInt16 nItemId, const sal_uInt16 nRegionItemId, const OUString &rName)
697 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
699 if (maRegions[i]->mnId == nRegionItemId)
701 TemplateContainerItem *pRegItem = maRegions[i];
703 std::vector<TemplateItemProperties>::iterator aIter;
704 for (aIter = pRegItem->maTemplates.begin(); aIter != pRegItem->maTemplates.end(); ++aIter)
706 if (aIter->nId == nItemId)
708 if (!mpDocTemplates->CopyTo(pRegItem->mnRegionId,aIter->nDocId,rName))
709 return false;
711 return true;
715 break;
719 return false;
722 bool TemplateLocalView::saveTemplateAs (sal_uInt16 nItemId,
723 com::sun::star::uno::Reference<com::sun::star::frame::XModel> &rModel,
724 const OUString &rName)
727 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
729 if (maRegions[i]->mnId == nItemId)
731 uno::Reference< frame::XStorable > xStorable(rModel, uno::UNO_QUERY_THROW );
733 uno::Reference< frame::XDocumentTemplates > xTemplates(
734 frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) );
736 if (!xTemplates->storeTemplate(mpDocTemplates->GetRegionName(maRegions[i]->mnRegionId),rName, xStorable ))
737 return false;
739 sal_uInt16 nDocId = maRegions[i]->maTemplates.size();
741 OUString aURL = mpDocTemplates->GetTemplateTargetURLFromComponent(mpDocTemplates->GetRegionName(maRegions[i]->mnRegionId),rName);
743 if(!mpDocTemplates->InsertTemplate(maRegions[i]->mnRegionId,nDocId,rName,aURL))
744 return false;
747 TemplateItemProperties aTemplate;
748 aTemplate.aIsFolder = false;
749 aTemplate.nId = getNextItemId();
750 aTemplate.nDocId = nDocId;
751 aTemplate.nRegionId = maRegions[i]->mnRegionId;
752 aTemplate.aName = rName;
753 aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(aURL,
754 TEMPLATE_THUMBNAIL_MAX_WIDTH,
755 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
756 aTemplate.aPath = aURL;
758 maRegions[i]->maTemplates.push_back(aTemplate);
760 insertItem(aTemplate);
762 return true;
766 return false;
769 bool TemplateLocalView::saveTemplateAs(TemplateContainerItem *pDstItem,
770 com::sun::star::uno::Reference<com::sun::star::frame::XModel> &rModel,
771 const OUString &rName)
773 uno::Reference< frame::XStorable > xStorable(rModel, uno::UNO_QUERY_THROW );
775 uno::Reference< frame::XDocumentTemplates > xTemplates(
776 frame::DocumentTemplates::create(comphelper::getProcessComponentContext()) );
778 if (!xTemplates->storeTemplate(mpDocTemplates->GetRegionName(pDstItem->mnRegionId),rName, xStorable ))
779 return false;
781 sal_uInt16 nDocId = pDstItem->maTemplates.size();
782 OUString aURL = mpDocTemplates->GetTemplateTargetURLFromComponent(mpDocTemplates->GetRegionName(pDstItem->mnRegionId),rName);
784 if(!mpDocTemplates->InsertTemplate(pDstItem->mnRegionId,nDocId,rName,aURL))
785 return false;
787 TemplateItemProperties aTemplate;
788 aTemplate.aIsFolder = false;
789 aTemplate.nId = pDstItem->maTemplates.empty() ? 1 : pDstItem->maTemplates.back().nId+1;
790 aTemplate.nDocId = nDocId;
791 aTemplate.nRegionId = pDstItem->mnRegionId;
792 aTemplate.aName = rName;
793 aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(aURL,
794 TEMPLATE_THUMBNAIL_MAX_WIDTH,
795 TEMPLATE_THUMBNAIL_MAX_HEIGHT);
796 aTemplate.aPath = aURL;
798 pDstItem->maTemplates.push_back(aTemplate);
800 return true;
803 bool TemplateLocalView::isTemplateNameUnique(const sal_uInt16 nRegionItemId, const OUString &rName) const
805 for (size_t i = 0, n = maRegions.size(); i < n; ++i)
807 if (maRegions[i]->mnId == nRegionItemId)
809 TemplateContainerItem *pRegItem = maRegions[i];
811 std::vector<TemplateItemProperties>::iterator aIter;
812 for (aIter = pRegItem->maTemplates.begin(); aIter != pRegItem->maTemplates.end(); ++aIter)
814 if (aIter->aName == rName)
815 return false;
818 break;
822 return true;
825 bool TemplateLocalView::renameItem(ThumbnailViewItem* pItem, OUString sNewTitle)
827 sal_uInt16 nRegionId = 0;
828 sal_uInt16 nDocId = USHRT_MAX;
829 TemplateViewItem* pDocItem = dynamic_cast<TemplateViewItem*>( pItem );
830 TemplateContainerItem* pContainerItem = dynamic_cast<TemplateContainerItem*>( pItem );
831 if ( pDocItem )
833 nRegionId = pDocItem->mnRegionId;
834 nDocId = pDocItem->mnDocId;
836 else if ( pContainerItem )
838 nRegionId = pContainerItem->mnRegionId;
840 return mpDocTemplates->SetName( sNewTitle, nRegionId, nDocId );
843 static void lcl_updateThumbnails (TemplateContainerItem *pItem)
845 pItem->maPreview1.Clear();
846 pItem->maPreview2.Clear();
847 pItem->maPreview3.Clear();
848 pItem->maPreview4.Clear();
850 // Update folder thumbnails
851 for (size_t i = 0, n = pItem->maTemplates.size(); i < n && pItem->HasMissingPreview(); ++i)
853 if ( pItem->maPreview1.IsEmpty( ) )
855 pItem->maPreview1 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail,
856 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
857 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
859 else if ( pItem->maPreview2.IsEmpty() )
861 pItem->maPreview2 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail,
862 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
863 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
865 else if ( pItem->maPreview3.IsEmpty() )
867 pItem->maPreview3 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail,
868 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
869 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
871 else if ( pItem->maPreview4.IsEmpty() )
873 pItem->maPreview4 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail,
874 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
875 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
880 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */