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/InteractionHandler.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 :
54 0 : Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
55 : Reference< XInteractionHandler > xGlobalInteractionHandler(
56 0 : InteractionHandler::createWithParent(xContext, 0), UNO_QUERY_THROW );
57 :
58 0 : m_xCmdEnv = new ucbhelper::CommandEnvironment( xGlobalInteractionHandler, Reference< XProgressHandler >() );
59 0 : }
60 :
61 0 : TemplateRemoteView::~TemplateRemoteView ()
62 : {
63 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
64 0 : delete maRepositories[i];
65 0 : }
66 :
67 0 : void TemplateRemoteView::Populate()
68 : {
69 0 : uno::Reference < uno::XComponentContext > m_context(comphelper::getProcessComponentContext());
70 :
71 : // Load from user settings
72 : com::sun::star::uno::Sequence<OUString> aUrls =
73 0 : officecfg::Office::Common::Misc::TemplateRepositoryUrls::get(m_context);
74 :
75 : com::sun::star::uno::Sequence<OUString> aNames =
76 0 : officecfg::Office::Common::Misc::TemplateRepositoryNames::get(m_context);
77 :
78 0 : for (sal_Int32 i = 0; i < aUrls.getLength() && i < aNames.getLength(); ++i)
79 : {
80 0 : TemplateRemoteViewItem *pItem = new TemplateRemoteViewItem(*this);
81 :
82 0 : pItem->mnId = i+1;
83 0 : pItem->maTitle = aNames[i];
84 0 : pItem->setURL(aUrls[i]);
85 :
86 0 : maRepositories.push_back(pItem);
87 0 : }
88 0 : }
89 :
90 0 : void TemplateRemoteView::reload ()
91 : {
92 0 : loadRepository(mpItemView->getId(),true);
93 0 : }
94 :
95 0 : void TemplateRemoteView::filterTemplatesByApp(const FILTER_APPLICATION &eApp)
96 : {
97 0 : if (mpItemView->IsVisible())
98 0 : mpItemView->filterItems(ViewFilter_Application(eApp));
99 0 : }
100 :
101 0 : void TemplateRemoteView::showOverlay (bool bVisible)
102 : {
103 0 : mpItemView->Show(bVisible);
104 :
105 : // Clear items is the overlay is closed.
106 0 : if (!bVisible)
107 : {
108 0 : mpItemView->Clear();
109 : }
110 0 : }
111 :
112 0 : bool TemplateRemoteView::loadRepository (const sal_uInt16 nRepositoryId, bool bRefresh)
113 : {
114 0 : TemplateRemoteViewItem *pItem = NULL;
115 :
116 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
117 : {
118 0 : if (maRepositories[i]->mnId == nRepositoryId)
119 : {
120 0 : pItem = maRepositories[i];
121 0 : break;
122 : }
123 : }
124 :
125 0 : if (!pItem)
126 0 : return false;
127 :
128 0 : if (!pItem->getTemplates().empty() && !bRefresh)
129 : {
130 0 : mpItemView->InsertItems(pItem->getTemplates());
131 0 : return true;
132 : }
133 :
134 0 : mpItemView->Clear();
135 0 : mpItemView->setId(nRepositoryId);
136 0 : mpItemView->setName(pItem->maTitle);
137 :
138 0 : OUString aURL = static_cast<TemplateRemoteViewItem*>(pItem)->getURL();
139 :
140 : try
141 : {
142 :
143 0 : uno::Sequence<OUString> aProps(8);
144 :
145 0 : aProps[0] = "Title";
146 0 : aProps[1] = "Size";
147 0 : aProps[2] = "DateModified";
148 0 : aProps[3] = "DateCreated";
149 0 : aProps[4] = "TargetURL";
150 0 : aProps[5] = "IsHidden";
151 0 : aProps[6] = "IsRemote";
152 0 : aProps[7] = "IsRemoveable";
153 :
154 0 : ucbhelper::Content aContent(aURL, m_xCmdEnv, comphelper::getProcessComponentContext());
155 :
156 0 : uno::Reference< XResultSet > xResultSet;
157 0 : uno::Reference< XDynamicResultSet > xDynResultSet;
158 :
159 0 : ucbhelper::ResultSetInclude eInclude = ucbhelper::INCLUDE_DOCUMENTS_ONLY;
160 0 : xDynResultSet = aContent.createDynamicCursor( aProps, eInclude );
161 :
162 0 : if ( xDynResultSet.is() )
163 0 : xResultSet = xDynResultSet->getStaticResultSet();
164 :
165 0 : if ( xResultSet.is() )
166 : {
167 0 : pItem->clearTemplates();
168 :
169 0 : uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
170 0 : uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
171 :
172 0 : util::DateTime aDT;
173 0 : std::vector<TemplateItemProperties> aItems;
174 :
175 0 : sal_uInt16 nIdx = 0;
176 0 : while ( xResultSet->next() )
177 : {
178 0 : bool bIsHidden = xRow->getBoolean( ROW_IS_HIDDEN );
179 :
180 : // don't show hidden files or anything besides documents
181 0 : if ( !bIsHidden || xRow->wasNull() )
182 : {
183 0 : aDT = xRow->getTimestamp( ROW_DATE_MOD );
184 0 : bool bContainsDate = !xRow->wasNull();
185 :
186 0 : if ( !bContainsDate )
187 : {
188 0 : aDT = xRow->getTimestamp( ROW_DATE_CREATE );
189 0 : bContainsDate = !xRow->wasNull();
190 : }
191 :
192 0 : OUString aContentURL = xContentAccess->queryContentIdentifierString();
193 0 : OUString aTargetURL = xRow->getString( ROW_TARGET_URL );
194 0 : bool bHasTargetURL = !xRow->wasNull() && !aTargetURL.isEmpty();
195 :
196 0 : OUString sRealURL = bHasTargetURL ? aTargetURL : aContentURL;
197 :
198 0 : TemplateItemProperties aTemplateItem;
199 0 : aTemplateItem.nId = nIdx+1;
200 0 : aTemplateItem.nRegionId = pItem->mnId-1;
201 0 : aTemplateItem.aPath = sRealURL;
202 : aTemplateItem.aThumbnail = TemplateAbstractView::fetchThumbnail(sRealURL,
203 : TEMPLATE_THUMBNAIL_MAX_WIDTH,
204 0 : TEMPLATE_THUMBNAIL_MAX_HEIGHT);
205 : // pData->mbIsRemote = xRow->getBoolean( ROW_IS_REMOTE ) && !xRow->wasNull();
206 : // pData->mbIsRemoveable = xRow->getBoolean( ROW_IS_REMOVEABLE ) && !xRow->wasNull();
207 0 : aTemplateItem.aName = xRow->getString( ROW_TITLE );
208 : // pData->maSize = xRow->getLong( ROW_SIZE );
209 :
210 0 : if ( bHasTargetURL &&
211 0 : INetURLObject( aContentURL ).GetProtocol() == INET_PROT_VND_SUN_STAR_HIER )
212 : {
213 0 : ucbhelper::Content aCnt( aTargetURL, m_xCmdEnv, comphelper::getProcessComponentContext() );
214 :
215 : try
216 : {
217 : // aCnt.getPropertyValue("Size") >>= pData->maSize;
218 0 : aCnt.getPropertyValue("DateModified") >>= aDT;
219 : }
220 0 : catch (...)
221 0 : {}
222 : }
223 :
224 0 : pItem->insertTemplate(aTemplateItem);
225 0 : aItems.push_back(aTemplateItem);
226 0 : ++nIdx;
227 : }
228 : }
229 :
230 0 : mpItemView->InsertItems(aItems);
231 0 : }
232 : }
233 0 : catch( ucb::CommandAbortedException& )
234 : {
235 : }
236 0 : catch( uno::RuntimeException& )
237 : {
238 : }
239 0 : catch( uno::Exception& )
240 : {
241 : }
242 :
243 0 : return true;
244 : }
245 :
246 0 : bool TemplateRemoteView::insertRepository(const OUString &rName, const OUString &rURL)
247 : {
248 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
249 : {
250 0 : if (maRepositories[i]->maTitle == rName)
251 0 : return false;
252 : }
253 :
254 0 : TemplateRemoteViewItem *pItem = new TemplateRemoteViewItem(*this);
255 :
256 0 : pItem->mnId = maRepositories.size()+1;
257 0 : pItem->maTitle = rName;
258 0 : pItem->setURL(rURL);
259 :
260 0 : maRepositories.push_back(pItem);
261 :
262 0 : mbIsSynced = false;
263 0 : return true;
264 : }
265 :
266 0 : bool TemplateRemoteView::deleteRepository(const sal_uInt16 nRepositoryId)
267 : {
268 0 : bool bRet = false;
269 :
270 0 : for (size_t i = 0, n = maRepositories.size(); i < n; ++i)
271 : {
272 0 : if (maRepositories[i]->mnId == nRepositoryId)
273 : {
274 0 : delete maRepositories[i];
275 :
276 0 : maRepositories.erase(maRepositories.begin() + i);
277 0 : mbIsSynced = false;
278 0 : bRet = true;
279 0 : break;
280 : }
281 : }
282 :
283 0 : return bRet;
284 : }
285 :
286 0 : void TemplateRemoteView::syncRepositories() const
287 : {
288 0 : if (!mbIsSynced)
289 : {
290 0 : uno::Reference < uno::XComponentContext > pContext(comphelper::getProcessComponentContext());
291 0 : boost::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create(pContext));
292 :
293 0 : size_t nSize = maRepositories.size();
294 0 : uno::Sequence<OUString> aUrls(nSize);
295 0 : uno::Sequence<OUString> aNames(nSize);
296 :
297 0 : for(size_t i = 0; i < nSize; ++i)
298 : {
299 0 : aUrls[i] = maRepositories[i]->getURL();
300 0 : aNames[i] = maRepositories[i]->maTitle;
301 : }
302 :
303 0 : officecfg::Office::Common::Misc::TemplateRepositoryUrls::set(aUrls, batch, pContext);
304 0 : officecfg::Office::Common::Misc::TemplateRepositoryNames::set(aNames, batch, pContext);
305 0 : batch->commit();
306 : }
307 0 : }
308 :
309 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|