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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <svx/PaletteManager.hxx>
21
22 #include <comphelper/propertyvalue.hxx>
23 #include <tools/urlobj.hxx>
24 #include <tools/json_writer.hxx>
25 #include <osl/file.hxx>
26 #include <unotools/pathoptions.hxx>
27 #include <sfx2/objsh.hxx>
28 #include <svx/drawitem.hxx>
29 #include <svx/strings.hrc>
30 #include <svx/svxids.hrc>
31 #include <svx/dialmgr.hxx>
32
33 #include <tbxcolorupdate.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/settings.hxx>
36 #include <comphelper/sequence.hxx>
37 #include <officecfg/Office/Common.hxx>
38 #include <com/sun/star/frame/XDispatchProvider.hpp>
39 #include <com/sun/star/frame/XDispatch.hpp>
40 #include <com/sun/star/frame/Desktop.hpp>
41 #include <com/sun/star/util/XURLTransformer.hpp>
42 #include <com/sun/star/util/URLTransformer.hpp>
43 #include <docmodel/color/ComplexColor.hxx>
44 #include <docmodel/color/ComplexColorJSON.hxx>
45
46 #include <palettes.hxx>
47
48 #include <memory>
49 #include <array>
50 #include <stack>
51
PaletteManager()52 PaletteManager::PaletteManager() :
53 mnMaxRecentColors(Application::GetSettings().GetStyleSettings().GetColorValueSetColumnCount()),
54 mnNumOfPalettes(3),
55 mnCurrentPalette(0),
56 mnColorCount(0),
57 mpBtnUpdater(nullptr),
58 maColorSelectFunction(PaletteManager::DispatchColorCommand)
59
60 {
61 SfxObjectShell* pDocSh = SfxObjectShell::Current();
62 if(pDocSh)
63 {
64 const SfxPoolItem* pItem = nullptr;
65 if( nullptr != ( pItem = pDocSh->GetItem(SID_COLOR_TABLE) ) )
66 mpColorList = pItem->StaticWhichCast(SID_COLOR_TABLE).GetColorList();
67 }
68 if(!mpColorList.is())
69 mpColorList = XColorList::CreateStdColorList();
70 LoadPalettes();
71 mnNumOfPalettes += m_Palettes.size();
72 }
73
~PaletteManager()74 PaletteManager::~PaletteManager()
75 {
76 }
77
LoadPalettes()78 void PaletteManager::LoadPalettes()
79 {
80 m_Palettes.clear();
81 OUString aPalPaths = SvtPathOptions().GetPalettePath();
82
83 std::stack<OUString> aDirs;
84 sal_Int32 nIndex = 0;
85 do
86 {
87 aDirs.push(aPalPaths.getToken(0, ';', nIndex));
88 }
89 while (nIndex >= 0);
90
91 std::set<OUString> aNames;
92 //try all entries palette path list user first, then
93 //system, ignoring duplicate file names
94 while (!aDirs.empty())
95 {
96 OUString aPalPath = aDirs.top();
97 aDirs.pop();
98
99 osl::Directory aDir(aPalPath);
100 osl::DirectoryItem aDirItem;
101 osl::FileStatus aFileStat( osl_FileStatus_Mask_FileName |
102 osl_FileStatus_Mask_FileURL |
103 osl_FileStatus_Mask_Type );
104 if( aDir.open() == osl::FileBase::E_None )
105 {
106 while( aDir.getNextItem(aDirItem) == osl::FileBase::E_None )
107 {
108 aDirItem.getFileStatus(aFileStat);
109 if(aFileStat.isRegular() || aFileStat.isLink())
110 {
111 OUString aFName = aFileStat.getFileName();
112 INetURLObject aURLObj( aFileStat.getFileURL() );
113 OUString aFNameWithoutExt = aURLObj.GetBase();
114 if (aNames.find(aFName) == aNames.end())
115 {
116 std::unique_ptr<Palette> pPalette;
117 if( aFName.endsWithIgnoreAsciiCase(".gpl") )
118 pPalette.reset(new PaletteGPL(aFileStat.getFileURL(), aFNameWithoutExt));
119 else if( aFName.endsWithIgnoreAsciiCase(".soc") )
120 {
121 if (aFNameWithoutExt == "standard")
122 aFNameWithoutExt = SvxResId(RID_SVXSTR_COLOR_PALETTE_STANDARD);
123 else if (aFNameWithoutExt == "tonal")
124 aFNameWithoutExt = SvxResId(RID_SVXSTR_COLOR_PALETTE_TONAL);
125 else if (aFNameWithoutExt == "html")
126 aFNameWithoutExt = SvxResId(RID_SVXSTR_COLOR_PALETTE_HTML);
127 else if (aFNameWithoutExt == "chart-palettes")
128 aFNameWithoutExt = SvxResId(RID_SVXSTR_COLOR_PALETTE_CHARTPALETTES);
129 else if (aFNameWithoutExt == "compatibility")
130 aFNameWithoutExt = SvxResId(RID_SVXSTR_COLOR_PALETTE_COMPATIBILITY);
131 else if (aFNameWithoutExt == "material")
132 aFNameWithoutExt = SvxResId(RID_SVXSTR_COLOR_PALETTE_MATERIAL);
133 else if (aFNameWithoutExt == "libreoffice")
134 aFNameWithoutExt = "LibreOffice";
135 else if (aFNameWithoutExt == "freecolour-hlc")
136 aFNameWithoutExt = SvxResId(RID_SVXSTR_COLOR_PALETTE_FREECOLOURHLC);
137 pPalette.reset(new PaletteSOC(aFileStat.getFileURL(), aFNameWithoutExt));
138 }
139 else if ( aFName.endsWithIgnoreAsciiCase(".ase") )
140 pPalette.reset(new PaletteASE(aFileStat.getFileURL(), aFNameWithoutExt));
141
142 if( pPalette && pPalette->IsValid() )
143 m_Palettes.push_back( std::move(pPalette) );
144
145 aNames.insert(aFNameWithoutExt);
146 }
147 }
148 }
149 }
150 }
151 }
152
IsThemePaletteSelected() const153 bool PaletteManager::IsThemePaletteSelected() const
154 {
155 return mnCurrentPalette == mnNumOfPalettes - 2;
156 }
157
GetThemeAndEffectIndex(sal_uInt16 nItemId,sal_uInt16 & rThemeIndex,sal_uInt16 & rEffectIndex)158 bool PaletteManager::GetThemeAndEffectIndex(sal_uInt16 nItemId, sal_uInt16& rThemeIndex, sal_uInt16& rEffectIndex)
159 {
160 // tdf#157034, nItemId begins with 1 but list of themes begin with 0
161 // so decrement nItemId
162 --nItemId;
163
164 // Each column is the same color with different effects.
165 rThemeIndex = nItemId % 12;
166
167 rEffectIndex = nItemId / 12;
168 if (rEffectIndex > 5)
169 return false;
170 return true;
171 }
172
GetLumModOff(sal_uInt16 nThemeIndex,sal_uInt16 nEffect,sal_Int16 & rLumMod,sal_Int16 & rLumOff)173 bool PaletteManager::GetLumModOff(sal_uInt16 nThemeIndex, sal_uInt16 nEffect, sal_Int16& rLumMod, sal_Int16& rLumOff)
174 {
175 if (!moThemePaletteCollection)
176 return false;
177
178 auto const& aThemeColorData = moThemePaletteCollection->maColors[nThemeIndex];
179
180 rLumMod = aThemeColorData.getLumMod(nEffect);
181 rLumOff = aThemeColorData.getLumOff(nEffect);
182
183 return true;
184 }
185
ReloadColorSet(SvxColorValueSet & rColorSet)186 void PaletteManager::ReloadColorSet(SvxColorValueSet &rColorSet)
187 {
188 moThemePaletteCollection.reset();
189 if( mnCurrentPalette == 0)
190 {
191 rColorSet.Clear();
192 css::uno::Sequence< sal_Int32 > CustomColorList( officecfg::Office::Common::UserColors::CustomColor::get() );
193 css::uno::Sequence< OUString > CustomColorNameList( officecfg::Office::Common::UserColors::CustomColorName::get() );
194 int nIx = 1;
195 for (int i = 0; i < CustomColorList.getLength(); ++i)
196 {
197 Color aColor(ColorTransparency, CustomColorList[i]);
198 rColorSet.InsertItem(nIx, aColor, CustomColorNameList[i]);
199 ++nIx;
200 }
201 }
202 else if (IsThemePaletteSelected())
203 {
204 SfxObjectShell* pObjectShell = SfxObjectShell::Current();
205 if (pObjectShell)
206 {
207 auto pColorSet = pObjectShell->GetThemeColors();
208 mnColorCount = 12;
209 rColorSet.Clear();
210 sal_uInt16 nItemId = 1;
211
212 if (!pColorSet)
213 return;
214
215 svx::ThemeColorPaletteManager aThemeColorManager(pColorSet);
216 moThemePaletteCollection = aThemeColorManager.generate();
217
218 // Each row is one effect type (no effect + each type).
219 for (size_t nEffect : {0, 1, 2, 3, 4, 5})
220 {
221 // Each column is one color type.
222 for (auto const& rColorData : moThemePaletteCollection->maColors)
223 {
224 auto const& rEffect = rColorData.maEffects[nEffect];
225 rColorSet.InsertItem(nItemId++, rEffect.maColor, rEffect.maColorName);
226 }
227 }
228 }
229 }
230 else if( mnCurrentPalette == mnNumOfPalettes - 1 )
231 {
232 // Add doc colors to palette
233 SfxObjectShell* pDocSh = SfxObjectShell::Current();
234 if (pDocSh)
235 {
236 std::set<Color> aColors = pDocSh->GetDocColors();
237 mnColorCount = aColors.size();
238 rColorSet.Clear();
239 rColorSet.addEntriesForColorSet(aColors, Concat2View(SvxResId( RID_SVXSTR_DOC_COLOR_PREFIX ) + " ") );
240 }
241 }
242 else
243 {
244 m_Palettes[mnCurrentPalette - 1]->LoadColorSet( rColorSet );
245 mnColorCount = rColorSet.GetItemCount();
246 }
247 }
248
ReloadRecentColorSet(SvxColorValueSet & rColorSet)249 void PaletteManager::ReloadRecentColorSet(SvxColorValueSet& rColorSet)
250 {
251 maRecentColors.clear();
252 rColorSet.Clear();
253 css::uno::Sequence< sal_Int32 > Colorlist(officecfg::Office::Common::UserColors::RecentColor::get());
254 css::uno::Sequence< OUString > ColorNamelist(officecfg::Office::Common::UserColors::RecentColorName::get());
255 int nIx = 1;
256 const bool bHasColorNames = Colorlist.getLength() == ColorNamelist.getLength();
257 for (int i = 0; i < Colorlist.getLength(); ++i)
258 {
259 Color aColor(ColorTransparency, Colorlist[i]);
260 OUString sColorName = bHasColorNames ? ColorNamelist[i] : ("#" + aColor.AsRGBHexString().toAsciiUpperCase());
261 maRecentColors.emplace_back(aColor, sColorName);
262 rColorSet.InsertItem(nIx, aColor, sColorName);
263 ++nIx;
264 }
265 }
266
GetPaletteList()267 std::vector<OUString> PaletteManager::GetPaletteList()
268 {
269 std::vector<OUString> aPaletteNames
270 {
271 SvxResId( RID_SVXSTR_CUSTOM_PAL )
272 };
273 for (auto const& it : m_Palettes)
274 {
275 aPaletteNames.push_back( (*it).GetName() );
276 }
277 aPaletteNames.push_back(SvxResId(RID_SVXSTR_THEME_COLORS));
278 aPaletteNames.push_back( SvxResId ( RID_SVXSTR_DOC_COLORS ) );
279
280 return aPaletteNames;
281 }
282
SetPalette(sal_Int32 nPos)283 void PaletteManager::SetPalette( sal_Int32 nPos )
284 {
285 mnCurrentPalette = nPos;
286 if( nPos != mnNumOfPalettes - 1 && nPos != 0)
287 {
288 mpColorList = XPropertyList::AsColorList(
289 XPropertyList::CreatePropertyListFromURL(
290 XPropertyListType::Color, GetSelectedPalettePath()));
291 auto name = GetPaletteName(); // may change pColorList
292 mpColorList->SetName(name);
293 if(mpColorList->Load())
294 {
295 SfxObjectShell* pShell = SfxObjectShell::Current();
296 if (pShell != nullptr)
297 {
298 SvxColorListItem aColorItem(mpColorList, SID_COLOR_TABLE);
299 pShell->PutItem( aColorItem );
300 }
301 }
302 }
303 OUString aPaletteName(officecfg::Office::Common::UserColors::PaletteName::get());
304 if (aPaletteName != GetPaletteName())
305 {
306 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
307 officecfg::Office::Common::UserColors::PaletteName::set(GetPaletteName(), batch);
308 batch->commit();
309 }
310 }
311
GetPalette() const312 sal_Int32 PaletteManager::GetPalette() const
313 {
314 return mnCurrentPalette;
315 }
316
GetPaletteName()317 OUString PaletteManager::GetPaletteName()
318 {
319 std::vector<OUString> aNames(GetPaletteList());
320 if(mnCurrentPalette != mnNumOfPalettes - 1 && mnCurrentPalette != 0)
321 {
322 SfxObjectShell* pDocSh = SfxObjectShell::Current();
323 if(pDocSh)
324 {
325 const SfxPoolItem* pItem = nullptr;
326 if( nullptr != ( pItem = pDocSh->GetItem(SID_COLOR_TABLE) ) )
327 mpColorList = pItem->StaticWhichCast(SID_COLOR_TABLE).GetColorList();
328 }
329 }
330 return aNames[mnCurrentPalette];
331 }
332
GetSelectedPalettePath()333 const OUString & PaletteManager::GetSelectedPalettePath()
334 {
335 if (mnCurrentPalette < m_Palettes.size() && mnCurrentPalette != 0)
336 return m_Palettes[mnCurrentPalette - 1]->GetPath();
337 else
338 return EMPTY_OUSTRING;
339 }
340
GetColorCount() const341 tools::Long PaletteManager::GetColorCount() const
342 {
343 return mnColorCount;
344 }
345
GetRecentColorCount() const346 tools::Long PaletteManager::GetRecentColorCount() const
347 {
348 return maRecentColors.size();
349 }
350
AddRecentColor(const Color & rRecentColor,const OUString & rName,bool bFront)351 void PaletteManager::AddRecentColor(const Color& rRecentColor, const OUString& rName, bool bFront)
352 {
353 auto itColor = std::find_if(maRecentColors.begin(),
354 maRecentColors.end(),
355 [rRecentColor] (const NamedColor &aColor) { return aColor.m_aColor == rRecentColor; });
356 // if recent color to be added is already in list, remove it
357 if( itColor != maRecentColors.end() )
358 maRecentColors.erase( itColor );
359
360 if (maRecentColors.size() == mnMaxRecentColors)
361 maRecentColors.pop_back();
362 if (bFront)
363 maRecentColors.emplace_front(rRecentColor, rName);
364 else
365 maRecentColors.emplace_back(rRecentColor, rName);
366 css::uno::Sequence< sal_Int32 > aColorList(maRecentColors.size());
367 auto aColorListRange = asNonConstRange(aColorList);
368 css::uno::Sequence< OUString > aColorNameList(maRecentColors.size());
369 auto aColorNameListRange = asNonConstRange(aColorNameList);
370 for (size_t i = 0; i < maRecentColors.size(); ++i)
371 {
372 aColorListRange[i] = static_cast<sal_Int32>(maRecentColors[i].m_aColor);
373 aColorNameListRange[i] = maRecentColors[i].m_aName;
374 }
375 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
376 officecfg::Office::Common::UserColors::RecentColor::set(aColorList, batch);
377 officecfg::Office::Common::UserColors::RecentColorName::set(aColorNameList, batch);
378 batch->commit();
379 }
380
SetSplitButtonColor(const NamedColor & rColor)381 void PaletteManager::SetSplitButtonColor(const NamedColor& rColor)
382 {
383 if (mpBtnUpdater)
384 mpBtnUpdater->SetRecentColor(rColor);
385 }
386
SetBtnUpdater(svx::ToolboxButtonColorUpdaterBase * pBtnUpdater)387 void PaletteManager::SetBtnUpdater(svx::ToolboxButtonColorUpdaterBase* pBtnUpdater)
388 {
389 mpBtnUpdater = pBtnUpdater;
390 }
391
SetColorSelectFunction(const ColorSelectFunction & aColorSelectFunction)392 void PaletteManager::SetColorSelectFunction(const ColorSelectFunction& aColorSelectFunction)
393 {
394 maColorSelectFunction = aColorSelectFunction;
395 }
396
PopupColorPicker(weld::Window * pParent,const OUString & aCommand,const Color & rInitialColor)397 void PaletteManager::PopupColorPicker(weld::Window* pParent, const OUString& aCommand, const Color& rInitialColor)
398 {
399 // The calling object goes away during aColorDlg.Execute(), so we must copy this
400 OUString aCommandCopy = aCommand;
401 m_pColorDlg = std::make_unique<ColorDialog>(pParent, vcl::ColorPickerMode::Modify);
402 m_pColorDlg->SetColor(rInitialColor);
403 std::shared_ptr<PaletteManager> xSelf(shared_from_this());
404 m_pColorDlg->ExecuteAsync([xSelf=std::move(xSelf),
405 aCommandCopy=std::move(aCommandCopy)] (sal_Int32 nResult) {
406 if (nResult == RET_OK)
407 {
408 Color aLastColor = xSelf->m_pColorDlg->GetColor();
409 OUString sColorName = "#" + aLastColor.AsRGBHexString().toAsciiUpperCase();
410 NamedColor aNamedColor(aLastColor, sColorName);
411 xSelf->SetSplitButtonColor(aNamedColor);
412 xSelf->AddRecentColor(aLastColor, sColorName);
413 xSelf->maColorSelectFunction(aCommandCopy, aNamedColor);
414 }
415 });
416 }
417
DispatchColorCommand(const OUString & aCommand,const NamedColor & rColor)418 void PaletteManager::DispatchColorCommand(const OUString& aCommand, const NamedColor& rColor)
419 {
420 using namespace css;
421 using namespace css::uno;
422 using namespace css::frame;
423 using namespace css::beans;
424 using namespace css::util;
425
426 const Reference<XComponentContext>& xContext(comphelper::getProcessComponentContext());
427 Reference<XDesktop2> xDesktop = Desktop::create(xContext);
428 Reference<XFrame> xFrame(xDesktop->getCurrentFrame());
429 Reference<XDispatchProvider> xDispatchProvider(xFrame, UNO_QUERY);
430 if (!xDispatchProvider.is())
431 return;
432
433 INetURLObject aObj( aCommand );
434
435 std::vector<PropertyValue> aArgs{
436 comphelper::makePropertyValue(aObj.GetURLPath()+ ".Color", sal_Int32(rColor.m_aColor)),
437 };
438
439 if (rColor.m_nThemeIndex != -1)
440 {
441 model::ComplexColor aComplexColor;
442 aComplexColor.setThemeColor(model::convertToThemeColorType(rColor.m_nThemeIndex));
443 if (rColor.m_nLumMod != 10000)
444 aComplexColor.addTransformation({model::TransformationType::LumMod, rColor.m_nLumMod});
445 if (rColor.m_nLumMod != 0)
446 aComplexColor.addTransformation({model::TransformationType::LumOff, rColor.m_nLumOff});
447
448 uno::Any aAny;
449 aAny <<= OStringToOUString(model::color::convertToJSON(aComplexColor), RTL_TEXTENCODING_UTF8);
450
451 aArgs.push_back(comphelper::makePropertyValue(aObj.GetURLPath() + ".ComplexColorJSON", aAny));
452 }
453
454 URL aTargetURL;
455 aTargetURL.Complete = aCommand;
456 Reference<XURLTransformer> xURLTransformer(URLTransformer::create(comphelper::getProcessComponentContext()));
457 xURLTransformer->parseStrict(aTargetURL);
458
459 Reference<XDispatch> xDispatch = xDispatchProvider->queryDispatch(aTargetURL, OUString(), 0);
460 if (xDispatch.is())
461 {
462 xDispatch->dispatch(aTargetURL, comphelper::containerToSequence(aArgs));
463 if (xFrame->getContainerWindow().is())
464 xFrame->getContainerWindow()->setFocus();
465 }
466 }
467
generateColorNamesJSON(tools::JsonWriter & aTree)468 void PaletteManager::generateColorNamesJSON(tools::JsonWriter& aTree)
469 {
470 XColorListRef xUserColorList;
471 OUString aPaletteStandard = SvxResId(RID_SVXSTR_COLOR_PALETTE_STANDARD);
472 PaletteManager aPaletteManager;
473 std::vector<OUString> aPaletteNames = aPaletteManager.GetPaletteList();
474 for (size_t i = 0, nLen = aPaletteNames.size(); i < nLen; ++i)
475 {
476 if (aPaletteStandard == aPaletteNames[i])
477 {
478 aPaletteManager.SetPalette(i);
479 xUserColorList
480 = XPropertyList::AsColorList(XPropertyList::CreatePropertyListFromURL(
481 XPropertyListType::Color, aPaletteManager.GetSelectedPalettePath()));
482 if (!xUserColorList->Load())
483 xUserColorList = nullptr;
484 break;
485 }
486 }
487 if (xUserColorList)
488 {
489 auto colorNames = aTree.startArray("ColorNames");
490 int nCount = xUserColorList->Count();
491
492 for (int i = 0; i < nCount; i++)
493 {
494 XColorEntry* pColorEntry = xUserColorList->GetColor(i);
495 if (pColorEntry)
496 {
497 auto aColorTree = aTree.startStruct();
498 aTree.put("hexCode", pColorEntry->GetColor().AsRGBHEXString());
499 aTree.put("name", pColorEntry->GetName());
500 }
501 }
502 }
503 }
504
505 // TODO: make it generic, send any palette
generateJSON(tools::JsonWriter & aTree,const std::set<Color> & rColors)506 void PaletteManager::generateJSON(tools::JsonWriter& aTree, const std::set<Color>& rColors)
507 {
508 auto aColorListTree = aTree.startArray("DocumentColors");
509 sal_uInt32 nStartIndex = 1;
510
511 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
512 sal_uInt32 nColumnCount = rStyleSettings.GetColorValueSetColumnCount();
513 const OUString aNamePrefix(Concat2View(SvxResId(RID_SVXSTR_DOC_COLOR_PREFIX) + " "));
514
515 auto aColorIt = rColors.begin();
516 while (aColorIt != rColors.end())
517 {
518 auto aColorRowTree = aTree.startAnonArray();
519
520 for (sal_uInt32 nColumn = 0; nColumn < nColumnCount; nColumn++)
521 {
522 auto aColorTree = aTree.startStruct();
523 OUString sName = aNamePrefix + OUString::number(nStartIndex++);
524 aTree.put("Value", aColorIt->AsRGBHexString().toUtf8());
525 aTree.put("Name", sName);
526
527 aColorIt++;
528 if (aColorIt == rColors.end())
529 break;
530 }
531 }
532 }
533
534 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
535