Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : */
9 : :
10 : : #include <sfx2/templateremoteview.hxx>
11 : :
12 : : #include <comphelper/processfactory.hxx>
13 : : #include <officecfg/Office/Common.hxx>
14 : : #include <sfx2/templateremoteviewitem.hxx>
15 : : #include <sfx2/templateview.hxx>
16 : : #include <sfx2/templateviewitem.hxx>
17 : : #include <svtools/imagemgr.hxx>
18 : : #include <tools/urlobj.hxx>
19 : : #include <ucbhelper/content.hxx>
20 : : #include <ucbhelper/commandenvironment.hxx>
21 : :
22 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : : #include <com/sun/star/task/XInteractionHandler.hpp>
24 : : #include <com/sun/star/sdbc/XResultSet.hpp>
25 : : #include <com/sun/star/sdbc/XRow.hpp>
26 : : #include <com/sun/star/ucb/XContentAccess.hpp>
27 : : #include <com/sun/star/ucb/XDynamicResultSet.hpp>
28 : :
29 : : using namespace com::sun::star;
30 : : using namespace com::sun::star::lang;
31 : : using namespace com::sun::star::task;
32 : : using namespace com::sun::star::sdbc;
33 : : using namespace com::sun::star::ucb;
34 : : using namespace com::sun::star::uno;
35 : :
36 : : enum
37 : : {
38 : : ROW_TITLE = 1,
39 : : ROW_SIZE,
40 : : ROW_DATE_MOD,
41 : : ROW_DATE_CREATE,
42 : : ROW_TARGET_URL,
43 : : ROW_IS_HIDDEN,
44 : : ROW_IS_REMOTE,
45 : : ROW_IS_REMOVEABLE
46 : : };
47 : :
48 : 0 : TemplateRemoteView::TemplateRemoteView (Window *pParent, WinBits nWinStyle, bool bDisableTransientChildren)
49 : : : TemplateAbstractView(pParent,nWinStyle,bDisableTransientChildren)
50 [ # # ][ # # ]: 0 : , mbIsSynced(true)
51 : : {
52 [ # # ]: 0 : mpItemView->SetColor(Color(COL_WHITE));
53 [ # # ]: 0 : mpItemView->setChangeNameHdl(LINK(this,TemplateRemoteView,ChangeNameHdl));
54 : :
55 [ # # ]: 0 : Reference< XMultiServiceFactory > xFactory = comphelper::getProcessServiceFactory();
56 : : Reference< XInteractionHandler > xGlobalInteractionHandler = Reference< XInteractionHandler >(
57 [ # # ][ # # ]: 0 : xFactory->createInstance("com.sun.star.task.InteractionHandler" ), UNO_QUERY );
[ # # ]
58 : :
59 [ # # ][ # # ]: 0 : m_xCmdEnv = new ucbhelper::CommandEnvironment( xGlobalInteractionHandler, Reference< XProgressHandler >() );
[ # # ]
60 : 0 : }
61 : :
62 : 0 : TemplateRemoteView::~TemplateRemoteView ()
63 : : {
64 [ # # ]: 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
65 [ # # ][ # # ]: 0 : delete maRepositories[i];
66 [ # # ]: 0 : }
67 : :
68 : 0 : void TemplateRemoteView::Populate()
69 : : {
70 [ # # ]: 0 : uno::Reference < uno::XComponentContext > m_context(comphelper::getProcessComponentContext());
71 : :
72 : : // Load from user settings
73 : : com::sun::star::uno::Sequence<OUString> aUrls =
74 [ # # ]: 0 : officecfg::Office::Common::Misc::TemplateRepositoryUrls::get(m_context);
75 : :
76 : : com::sun::star::uno::Sequence<OUString> aNames =
77 [ # # ]: 0 : officecfg::Office::Common::Misc::TemplateRepositoryNames::get(m_context);
78 : :
79 [ # # ][ # # ]: 0 : for (sal_Int32 i = 0; i < aUrls.getLength() && i < aNames.getLength(); ++i)
[ # # ]
80 : : {
81 [ # # ][ # # ]: 0 : TemplateRemoteViewItem *pItem = new TemplateRemoteViewItem(*this,this);
82 : :
83 : 0 : pItem->mnId = i+1;
84 [ # # ]: 0 : pItem->maTitle = aNames[i];
85 [ # # ]: 0 : pItem->setURL(aUrls[i]);
86 : :
87 [ # # ]: 0 : maRepositories.push_back(pItem);
88 [ # # ][ # # ]: 0 : }
89 : 0 : }
90 : :
91 : 0 : void TemplateRemoteView::reload ()
92 : : {
93 : 0 : loadRepository(mpItemView->getId(),true);
94 : 0 : }
95 : :
96 : 0 : void TemplateRemoteView::filterTemplatesByApp(const FILTER_APPLICATION &eApp)
97 : : {
98 [ # # ]: 0 : if (mpItemView->IsVisible())
99 [ # # ][ # # ]: 0 : mpItemView->filterItems(ViewFilter_Application(eApp));
[ # # ]
100 : 0 : }
101 : :
102 : 0 : void TemplateRemoteView::showOverlay (bool bVisible)
103 : : {
104 : 0 : mpItemView->Show(bVisible);
105 : :
106 : : // Clear items is the overlay is closed.
107 [ # # ]: 0 : if (!bVisible)
108 : : {
109 : 0 : mpItemView->Clear();
110 : :
111 : 0 : setSelectionMode(mbSelectionMode);
112 : : }
113 : 0 : }
114 : :
115 : 0 : void TemplateRemoteView::setOverlayChangeNameHdl(const Link &rLink)
116 : : {
117 : 0 : maChangeNameHdl = rLink;
118 : 0 : }
119 : :
120 : 0 : bool TemplateRemoteView::loadRepository (const sal_uInt16 nRepositoryId, bool bRefresh)
121 : : {
122 : 0 : TemplateRemoteViewItem *pItem = NULL;
123 : :
124 [ # # ]: 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
125 : : {
126 [ # # ]: 0 : if (maRepositories[i]->mnId == nRepositoryId)
127 : : {
128 : 0 : pItem = maRepositories[i];
129 : 0 : break;
130 : : }
131 : : }
132 : :
133 [ # # ]: 0 : if (!pItem)
134 : 0 : return false;
135 : :
136 [ # # ][ # # ]: 0 : if (!pItem->getTemplates().empty() && !bRefresh)
[ # # ]
137 : : {
138 [ # # ]: 0 : mpItemView->InsertItems(pItem->getTemplates());
139 : 0 : return true;
140 : : }
141 : :
142 [ # # ]: 0 : mpItemView->Clear();
143 : 0 : mpItemView->setId(nRepositoryId);
144 [ # # ]: 0 : mpItemView->setName(pItem->maTitle);
145 : :
146 : 0 : OUString aURL = static_cast<TemplateRemoteViewItem*>(pItem)->getURL();
147 : :
148 : : try
149 : : {
150 : :
151 [ # # ]: 0 : uno::Sequence<OUString> aProps(8);
152 : :
153 [ # # ]: 0 : aProps[0] = "Title";
154 [ # # ]: 0 : aProps[1] = "Size";
155 [ # # ]: 0 : aProps[2] = "DateModified";
156 [ # # ]: 0 : aProps[3] = "DateCreated";
157 [ # # ]: 0 : aProps[4] = "TargetURL";
158 [ # # ]: 0 : aProps[5] = "IsHidden";
159 [ # # ]: 0 : aProps[6] = "IsRemote";
160 [ # # ]: 0 : aProps[7] = "IsRemoveable";
161 : :
162 [ # # ]: 0 : ucbhelper::Content aContent(aURL,m_xCmdEnv);
163 : :
164 : 0 : uno::Reference< XResultSet > xResultSet;
165 : 0 : uno::Reference< XDynamicResultSet > xDynResultSet;
166 : :
167 : 0 : ucbhelper::ResultSetInclude eInclude = ucbhelper::INCLUDE_DOCUMENTS_ONLY;
168 [ # # ][ # # ]: 0 : xDynResultSet = aContent.createDynamicCursor( aProps, eInclude );
169 : :
170 [ # # ]: 0 : if ( xDynResultSet.is() )
171 [ # # ][ # # ]: 0 : xResultSet = xDynResultSet->getStaticResultSet();
[ # # ]
172 : :
173 [ # # ]: 0 : if ( xResultSet.is() )
174 : : {
175 : 0 : pItem->clearTemplates();
176 : :
177 [ # # ]: 0 : uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
178 [ # # ]: 0 : uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
179 : :
180 : 0 : util::DateTime aDT;
181 [ # # ]: 0 : std::vector<TemplateItemProperties> aItems;
182 : :
183 : 0 : sal_uInt16 nIdx = 0;
184 [ # # ][ # # ]: 0 : while ( xResultSet->next() )
[ # # ]
185 : : {
186 [ # # ][ # # ]: 0 : bool bIsHidden = xRow->getBoolean( ROW_IS_HIDDEN );
187 : :
188 : : // don't show hidden files or anything besides documents
189 [ # # ][ # # ]: 0 : if ( !bIsHidden || xRow->wasNull() )
[ # # ][ # # ]
[ # # ]
190 : : {
191 [ # # ][ # # ]: 0 : aDT = xRow->getTimestamp( ROW_DATE_MOD );
192 [ # # ][ # # ]: 0 : bool bContainsDate = !xRow->wasNull();
193 : :
194 [ # # ]: 0 : if ( !bContainsDate )
195 : : {
196 [ # # ][ # # ]: 0 : aDT = xRow->getTimestamp( ROW_DATE_CREATE );
197 [ # # ][ # # ]: 0 : bContainsDate = !xRow->wasNull();
198 : : }
199 : :
200 [ # # ][ # # ]: 0 : OUString aContentURL = xContentAccess->queryContentIdentifierString();
201 [ # # ][ # # ]: 0 : OUString aTargetURL = xRow->getString( ROW_TARGET_URL );
202 [ # # ][ # # ]: 0 : bool bHasTargetURL = !xRow->wasNull() && !aTargetURL.isEmpty();
[ # # ][ # # ]
203 : :
204 [ # # ]: 0 : OUString sRealURL = bHasTargetURL ? aTargetURL : aContentURL;
205 : :
206 [ # # ]: 0 : TemplateItemProperties aTemplateItem;
207 : 0 : aTemplateItem.nId = nIdx+1;
208 : 0 : aTemplateItem.nRegionId = pItem->mnId-1;
209 : 0 : aTemplateItem.aPath = sRealURL;
210 : : aTemplateItem.aThumbnail = TemplateAbstractView::fetchThumbnail(sRealURL,
211 : : TEMPLATE_THUMBNAIL_MAX_WIDTH,
212 [ # # ][ # # ]: 0 : TEMPLATE_THUMBNAIL_MAX_HEIGHT);
[ # # ]
213 : : // pData->mbIsRemote = xRow->getBoolean( ROW_IS_REMOTE ) && !xRow->wasNull();
214 : : // pData->mbIsRemoveable = xRow->getBoolean( ROW_IS_REMOVEABLE ) && !xRow->wasNull();
215 [ # # ][ # # ]: 0 : aTemplateItem.aName = xRow->getString( ROW_TITLE );
216 : : // pData->maSize = xRow->getLong( ROW_SIZE );
217 : :
218 [ # # ]: 0 : if ( bHasTargetURL &&
[ # # # # ]
219 [ # # ][ # # ]: 0 : INetURLObject( aContentURL ).GetProtocol() == INET_PROT_VND_SUN_STAR_HIER )
[ # # ][ # # ]
220 : : {
221 [ # # ]: 0 : ucbhelper::Content aCnt( aTargetURL, m_xCmdEnv );
222 : :
223 : : try
224 : : {
225 : : // aCnt.getPropertyValue("Size") >>= pData->maSize;
226 [ # # ][ # # ]: 0 : aCnt.getPropertyValue("DateModified") >>= aDT;
227 : : }
228 [ # # ]: 0 : catch (...)
229 [ # # ]: 0 : {}
230 : : }
231 : :
232 [ # # ][ # # ]: 0 : aTemplateItem.aType = SvFileInformationManager::GetFileDescription(INetURLObject(sRealURL));
[ # # ][ # # ]
[ # # ]
233 : :
234 [ # # ]: 0 : pItem->insertTemplate(aTemplateItem);
235 [ # # ]: 0 : aItems.push_back(aTemplateItem);
236 [ # # ]: 0 : ++nIdx;
237 : : }
238 : : }
239 : :
240 [ # # ]: 0 : mpItemView->InsertItems(aItems);
241 [ # # ][ # # ]: 0 : }
[ # # # # ]
242 : : }
243 [ # # ]: 0 : catch( ucb::CommandAbortedException& )
244 : : {
245 : : }
246 [ # # ]: 0 : catch( uno::RuntimeException& )
247 : : {
248 : : }
249 [ # # ]: 0 : catch( uno::Exception& )
250 : : {
251 : : }
252 : :
253 : 0 : return true;
254 : : }
255 : :
256 : 0 : bool TemplateRemoteView::insertRepository(const OUString &rName, const OUString &rURL)
257 : : {
258 [ # # ]: 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
259 : : {
260 [ # # ]: 0 : if (maRepositories[i]->maTitle == rName)
261 : 0 : return false;
262 : : }
263 : :
264 [ # # ][ # # ]: 0 : TemplateRemoteViewItem *pItem = new TemplateRemoteViewItem(*this,this);
265 : :
266 : 0 : pItem->mnId = maRepositories.size()+1;
267 : 0 : pItem->maTitle = rName;
268 : 0 : pItem->setURL(rURL);
269 : :
270 [ # # ]: 0 : maRepositories.push_back(pItem);
271 : :
272 : 0 : mbIsSynced = false;
273 : 0 : return true;
274 : : }
275 : :
276 : 0 : bool TemplateRemoteView::deleteRepository(const sal_uInt16 nRepositoryId)
277 : : {
278 : 0 : bool bRet = false;
279 : :
280 [ # # ]: 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
281 : : {
282 [ # # ]: 0 : if (maRepositories[i]->mnId == nRepositoryId)
283 : : {
284 [ # # ]: 0 : delete maRepositories[i];
285 : :
286 [ # # ][ # # ]: 0 : maRepositories.erase(maRepositories.begin() + i);
287 : 0 : mbIsSynced = false;
288 : 0 : bRet = true;
289 : 0 : break;
290 : : }
291 : : }
292 : :
293 : 0 : return bRet;
294 : : }
295 : :
296 : 0 : void TemplateRemoteView::syncRepositories() const
297 : : {
298 [ # # ]: 0 : if (!mbIsSynced)
299 : : {
300 [ # # ]: 0 : uno::Reference < uno::XComponentContext > pContext(comphelper::getProcessComponentContext());
301 [ # # ]: 0 : boost::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create(pContext));
302 : :
303 : 0 : size_t nSize = maRepositories.size();
304 [ # # ]: 0 : uno::Sequence<OUString> aUrls(nSize);
305 [ # # ]: 0 : uno::Sequence<OUString> aNames(nSize);
306 : :
307 [ # # ]: 0 : for(size_t i = 0; i < nSize; ++i)
308 : : {
309 [ # # ]: 0 : aUrls[i] = maRepositories[i]->getURL();
310 [ # # ]: 0 : aNames[i] = maRepositories[i]->maTitle;
311 : : }
312 : :
313 [ # # ]: 0 : officecfg::Office::Common::Misc::TemplateRepositoryUrls::set(aUrls, batch, pContext);
314 [ # # ]: 0 : officecfg::Office::Common::Misc::TemplateRepositoryNames::set(aNames, batch, pContext);
315 [ # # ][ # # ]: 0 : batch->commit();
[ # # ][ # # ]
316 : : }
317 : 0 : }
318 : :
319 : 0 : IMPL_LINK (TemplateRemoteView, ChangeNameHdl, TemplateView*, pView)
320 : : {
321 : 0 : bool bRet = false;
322 : :
323 : : // check if there isnt another repository with the same name.
324 [ # # ]: 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
325 : : {
326 [ # # ]: 0 : if (maRepositories[i]->mnId == pView->getId())
327 : : {
328 : 0 : maRepositories[i]->maTitle = pView->getName();
329 : :
330 : 0 : bRet = true;
331 : 0 : mbIsSynced = false;
332 : 0 : maChangeNameHdl.Call(this);
333 : 0 : break;
334 : : }
335 : : }
336 : :
337 : 0 : return bRet;
338 : : }
339 : :
340 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|