summaryrefslogtreecommitdiff
path: root/cui/source/customize/SvxConfigPageHelper.cxx
blob: 39044173b8ed43011442739491ce06f9afc02ef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <SvxConfigPageHelper.hxx>

#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
#include <com/sun/star/ui/ImageType.hpp>
#include <com/sun/star/ui/ItemType.hpp>

#include <comphelper/random.hxx>
#include <svtools/imgdef.hxx>
#include <svtools/miscopt.hxx>

static sal_Int16 theImageType = css::ui::ImageType::COLOR_NORMAL | css::ui::ImageType::SIZE_DEFAULT;

void SvxConfigPageHelper::RemoveEntry(SvxEntries* pEntries, SvxConfigEntry const* pChildEntry)
{
    SvxEntries::iterator iter = pEntries->begin();

    while (iter != pEntries->end())
    {
        if (pChildEntry == *iter)
        {
            pEntries->erase(iter);
            break;
        }
        ++iter;
    }
}

OUString SvxConfigPageHelper::replaceSaveInName(const OUString& rMessage,
                                                std::u16string_view rSaveInName)
{
    OUString name = rMessage.replaceFirst("%SAVE IN SELECTION%", rSaveInName);

    return name;
}

OUString SvxConfigPageHelper::stripHotKey(const OUString& str) { return str.replaceFirst("~", ""); }

OUString SvxConfigPageHelper::replaceSixteen(const OUString& str, sal_Int32 nReplacement)
{
    return str.replaceAll(OUString::number(16), OUString::number(nReplacement));
}

sal_Int16 SvxConfigPageHelper::GetImageType() { return theImageType; }

void SvxConfigPageHelper::InitImageType()
{
    theImageType = css::ui::ImageType::COLOR_NORMAL | css::ui::ImageType::SIZE_DEFAULT;

    if (SvtMiscOptions().GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_LARGE)
    {
        theImageType |= css::ui::ImageType::SIZE_LARGE;
    }
    else if (SvtMiscOptions().GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_32)
    {
        theImageType |= css::ui::ImageType::SIZE_32;
    }
}

css::uno::Reference<css::graphic::XGraphic>
SvxConfigPageHelper::GetGraphic(const css::uno::Reference<css::ui::XImageManager>& xImageManager,
                                const OUString& rCommandURL)
{
    css::uno::Reference<css::graphic::XGraphic> result;

    if (xImageManager.is())
    {
        // TODO handle large graphics
        css::uno::Sequence<css::uno::Reference<css::graphic::XGraphic>> aGraphicSeq;

        css::uno::Sequence<OUString> aImageCmdSeq{ rCommandURL };

        try
        {
            aGraphicSeq = xImageManager->getImages(GetImageType(), aImageCmdSeq);

            if (aGraphicSeq.hasElements())
            {
                result = aGraphicSeq[0];
            }
        }
        catch (css::uno::Exception&)
        {
            // will return empty XGraphic
        }
    }

    return result;
}

OUString SvxConfigPageHelper::generateCustomName(const OUString& prefix, SvxEntries* entries,
                                                 sal_Int32 suffix /*= 1*/)
{
    OUString name;
    sal_Int32 pos = 0;

    // find and replace the %n placeholder in the prefix string
    name = prefix.replaceFirst("%n", OUString::number(suffix), &pos);

    if (pos == -1)
    {
        // no placeholder found so just append the suffix
        name += OUString::number(suffix);
    }

    if (!entries)
        return name;

    // now check if there is an already existing entry with this name
    bool bFoundEntry = false;
    for (auto const& entry : *entries)
    {
        if (name.equals(entry->GetName()))
        {
            bFoundEntry = true;
            break;
        }
    }

    if (bFoundEntry)
    {
        // name already exists so try the next number up
        return generateCustomName(prefix, entries, ++suffix);
    }

    return name;
}

OUString SvxConfigPageHelper::generateCustomMenuURL(SvxEntries* entries, sal_Int32 suffix /*= 1*/)
{
    OUString url = "vnd.openoffice.org:CustomMenu" + OUString::number(suffix);
    if (!entries)
        return url;

    // now check is there is an already existing entry with this url
    bool bFoundEntry = false;
    for (auto const& entry : *entries)
    {
        if (url.equals(entry->GetCommand()))
        {
            bFoundEntry = true;
            break;
        }
    }

    if (bFoundEntry)
    {
        // url already exists so try the next number up
        return generateCustomMenuURL(entries, ++suffix);
    }

    return url;
}

sal_uInt32 SvxConfigPageHelper::generateRandomValue()
{
    return comphelper::rng::uniform_uint_distribution(0, std::numeric_limits<unsigned int>::max());
}

OUString SvxConfigPageHelper::generateCustomURL(SvxEntries* entries)
{
    OUString url = OUString::Concat(ITEM_TOOLBAR_URL) + CUSTOM_TOOLBAR_STR +
                   // use a random number to minimize possible clash with existing custom toolbars
                   OUString::number(generateRandomValue(), 16);

    // now check is there is an already existing entry with this url
    bool bFoundEntry = false;
    for (auto const& entry : *entries)
    {
        if (url.equals(entry->GetCommand()))
        {
            bFoundEntry = true;
            break;
        }
    }

    if (bFoundEntry)
    {
        // url already exists so try the next number up
        return generateCustomURL(entries);
    }

    return url;
}

OUString SvxConfigPageHelper::GetModuleName(std::u16string_view aModuleId)
{
    if (aModuleId == u"com.sun.star.text.TextDocument"
        || aModuleId == u"com.sun.star.text.GlobalDocument")
        return "Writer";
    else if (aModuleId == u"com.sun.star.text.WebDocument")
        return "Writer/Web";
    else if (aModuleId == u"com.sun.star.drawing.DrawingDocument")
        return "Draw";
    else if (aModuleId == u"com.sun.star.presentation.PresentationDocument")
        return "Impress";
    else if (aModuleId == u"com.sun.star.sheet.SpreadsheetDocument")
        return "Calc";
    else if (aModuleId == u"com.sun.star.script.BasicIDE")
        return "Basic";
    else if (aModuleId == u"com.sun.star.formula.FormulaProperties")
        return "Math";
    else if (aModuleId == u"com.sun.star.sdb.RelationDesign")
        return "Relation Design";
    else if (aModuleId == u"com.sun.star.sdb.QueryDesign")
        return "Query Design";
    else if (aModuleId == u"com.sun.star.sdb.TableDesign")
        return "Table Design";
    else if (aModuleId == u"com.sun.star.sdb.DataSourceBrowser")
        return "Data Source Browser";
    else if (aModuleId == u"com.sun.star.sdb.DatabaseDocument")
        return "Database";

    return OUString();
}

OUString SvxConfigPageHelper::GetUIModuleName(
    const OUString& aModuleId,
    const css::uno::Reference<css::frame::XModuleManager2>& rModuleManager)
{
    assert(rModuleManager.is());

    OUString aModuleUIName;

    try
    {
        css::uno::Any a = rModuleManager->getByName(aModuleId);
        css::uno::Sequence<css::beans::PropertyValue> aSeq;

        if (a >>= aSeq)
        {
            for (css::beans::PropertyValue const& rProp : std::as_const(aSeq))
            {
                if (rProp.Name == "ooSetupFactoryUIName")
                {
                    rProp.Value >>= aModuleUIName;
                    break;
                }
            }
        }
    }
    catch (css::uno::RuntimeException&)
    {
        throw;
    }
    catch (css::uno::Exception&)
    {
    }

    if (aModuleUIName.isEmpty())
        aModuleUIName = GetModuleName(aModuleId);

    return aModuleUIName;
}

bool SvxConfigPageHelper::GetMenuItemData(
    const css::uno::Reference<css::container::XIndexAccess>& rItemContainer, sal_Int32 nIndex,
    OUString& rCommandURL, OUString& rLabel, sal_uInt16& rType, sal_Int32& rStyle,
    css::uno::Reference<css::container::XIndexAccess>& rSubMenu)
{
    try
    {
        css::uno::Sequence<css::beans::PropertyValue> aProps;
        if (rItemContainer->getByIndex(nIndex) >>= aProps)
        {
            for (css::beans::PropertyValue const& rProp : std::as_const(aProps))
            {
                if (rProp.Name == ITEM_DESCRIPTOR_COMMANDURL)
                {
                    rProp.Value >>= rCommandURL;
                }
                else if (rProp.Name == ITEM_DESCRIPTOR_CONTAINER)
                {
                    rProp.Value >>= rSubMenu;
                }
                else if (rProp.Name == ITEM_DESCRIPTOR_STYLE)
                {
                    rProp.Value >>= rStyle;
                }
                else if (rProp.Name == ITEM_DESCRIPTOR_LABEL)
                {
                    rProp.Value >>= rLabel;
                }
                else if (rProp.Name == ITEM_DESCRIPTOR_TYPE)
                {
                    rProp.Value >>= rType;
                }
            }

            return true;
        }
    }
    catch (css::lang::IndexOutOfBoundsException&)
    {
    }

    return false;
}

bool SvxConfigPageHelper::GetToolbarItemData(
    const css::uno::Reference<css::container::XIndexAccess>& rItemContainer, sal_Int32 nIndex,
    OUString& rCommandURL, OUString& rLabel, sal_uInt16& rType, bool& rIsVisible, sal_Int32& rStyle)
{
    try
    {
        css::uno::Sequence<css::beans::PropertyValue> aProps;
        if (rItemContainer->getByIndex(nIndex) >>= aProps)
        {
            for (css::beans::PropertyValue const& rProp : std::as_const(aProps))
            {
                if (rProp.Name == ITEM_DESCRIPTOR_COMMANDURL)
                {
                    rProp.Value >>= rCommandURL;
                }
                else if (rProp.Name == ITEM_DESCRIPTOR_STYLE)
                {
                    rProp.Value >>= rStyle;
                }
                else if (rProp.Name == ITEM_DESCRIPTOR_LABEL)
                {
                    rProp.Value >>= rLabel;
                }
                else if (rProp.Name == ITEM_DESCRIPTOR_TYPE)
                {
                    rProp.Value >>= rType;
                }
                else if (rProp.Name == ITEM_DESCRIPTOR_ISVISIBLE)
                {
                    rProp.Value >>= rIsVisible;
                }
            }

            return true;
        }
    }
    catch (css::lang::IndexOutOfBoundsException&)
    {
    }

    return false;
}

css::uno::Sequence<css::beans::PropertyValue>
SvxConfigPageHelper::ConvertSvxConfigEntry(const SvxConfigEntry* pEntry)
{
    css::uno::Sequence<css::beans::PropertyValue> aPropSeq(4);

    aPropSeq[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
    aPropSeq[0].Value <<= pEntry->GetCommand();

    aPropSeq[1].Name = ITEM_DESCRIPTOR_TYPE;
    aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;

    // If the name has not been changed, then the label can be stored
    // as an empty string.
    // It will be initialised again later using the command to label map.
    aPropSeq[2].Name = ITEM_DESCRIPTOR_LABEL;
    if (!pEntry->HasChangedName() && !pEntry->GetCommand().isEmpty())
    {
        aPropSeq[2].Value <<= OUString();
    }
    else
    {
        aPropSeq[2].Value <<= pEntry->GetName();
    }

    aPropSeq[3].Name = ITEM_DESCRIPTOR_STYLE;
    aPropSeq[3].Value <<= static_cast<sal_Int16>(pEntry->GetStyle());

    return aPropSeq;
}

css::uno::Sequence<css::beans::PropertyValue>
SvxConfigPageHelper::ConvertToolbarEntry(const SvxConfigEntry* pEntry)
{
    css::uno::Sequence<css::beans::PropertyValue> aPropSeq(5);

    aPropSeq[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
    aPropSeq[0].Value <<= pEntry->GetCommand();

    aPropSeq[1].Name = ITEM_DESCRIPTOR_TYPE;
    aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;

    // If the name has not been changed, then the label can be stored
    // as an empty string.
    // It will be initialised again later using the command to label map.
    aPropSeq[2].Name = ITEM_DESCRIPTOR_LABEL;
    if (!pEntry->HasChangedName() && !pEntry->GetCommand().isEmpty())
    {
        aPropSeq[2].Value <<= OUString();
    }
    else
    {
        aPropSeq[2].Value <<= pEntry->GetName();
    }

    aPropSeq[3].Name = ITEM_DESCRIPTOR_ISVISIBLE;
    aPropSeq[3].Value <<= pEntry->IsVisible();

    aPropSeq[4].Name = ITEM_DESCRIPTOR_STYLE;
    aPropSeq[4].Value <<= static_cast<sal_Int16>(pEntry->GetStyle());

    return aPropSeq;
}

bool SvxConfigPageHelper::EntrySort(SvxConfigEntry const* a, SvxConfigEntry const* b)
{
    return a->GetName().compareTo(b->GetName()) < 0;
}

bool SvxConfigPageHelper::SvxConfigEntryModified(SvxConfigEntry const* pEntry)
{
    SvxEntries* pEntries = pEntry->GetEntries();
    if (!pEntries)
        return false;

    for (const auto& entry : *pEntries)
    {
        if (entry->IsModified() || SvxConfigEntryModified(entry))
            return true;
    }
    return false;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */