manager.cpp 31.5 KB
Newer Older
1 2
/******************************************************************************
 *
3
 *  SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4
 *
5
 *  SPDX-License-Identifier: GPL-2.0-or-later
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *******************************************************************************/

#include "core/manager.h"

#include "core/aggregation.h"
#include "core/theme.h"
#include "core/view.h"
#include "core/widgetbase.h"
#include "core/storagemodelbase.h"
#include "core/model.h"
#include "core/model_p.h"
#include "messagelistsettings.h"

#include "utils/configureaggregationsdialog.h"
#include "utils/configureaggregationsdialog_p.h"
#include "utils/configurethemesdialog.h"
#include "utils/configurethemesdialog_p.h"

#include "MessageCore/MessageCoreSettings"

#include "messagelistutil.h"
Laurent Montel's avatar
Laurent Montel committed
28
#include "messagelistutil_p.h"
29 30 31 32 33 34 35 36 37

#include <kmime/kmime_dateformatter.h> // kdepimlibs

#include <KConfig>
#include "messagelist_debug.h"
#include <KLocalizedString>

using namespace MessageList::Core;

Laurent Montel's avatar
Laurent Montel committed
38
Manager *Manager::mInstance = nullptr;
39 40 41 42 43 44 45 46 47 48 49

Manager::Manager()
    : QObject()
{
    mInstance = this;

    mDateFormatter = new KMime::DateFormatter();

    mCachedLocalizedUnknownText = i18nc("Unknown date", "Unknown");

    loadConfiguration();
Daniel Vrátil's avatar
Daniel Vrátil committed
50 51 52 53
    connect(MessageListSettings::self(), &MessageListSettings::configChanged,
            this, &Manager::reloadGlobalConfiguration);
    connect(MessageCore::MessageCoreSettings::self(), &MessageCore::MessageCoreSettings::configChanged,
            this, &Manager::reloadGlobalConfiguration);
54 55 56 57
}

Manager::~Manager()
{
58 59 60 61 62
    disconnect(MessageListSettings::self(), &MessageListSettings::configChanged,
            this, &Manager::reloadGlobalConfiguration);
    disconnect(MessageCore::MessageCoreSettings::self(), &MessageCore::MessageCoreSettings::configChanged,
            this, &Manager::reloadGlobalConfiguration);

63 64 65 66 67 68
    saveConfiguration();
    removeAllAggregations();
    removeAllThemes();

    delete mDateFormatter;

Laurent Montel's avatar
Laurent Montel committed
69
    mInstance = nullptr;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
}

void Manager::registerWidget(Widget *pWidget)
{
    if (!mInstance) {
        mInstance = new Manager();
    }

    mInstance->mWidgetList.append(pWidget);
}

void Manager::unregisterWidget(Widget *pWidget)
{
    if (!mInstance) {
        qCWarning(MESSAGELIST_LOG) << ("ERROR: MessageList::Manager::unregisterWidget() called when Manager::mInstance is 0");
        return;
    }

    mInstance->mWidgetList.removeAll(pWidget);

    if (mInstance->mWidgetList.isEmpty()) {
        delete mInstance;
Laurent Montel's avatar
Laurent Montel committed
92
        mInstance = nullptr;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }
}

const Aggregation *Manager::aggregation(const QString &id)
{
    Aggregation *opt = mAggregations.value(id);
    if (opt) {
        return opt;
    }

    return defaultAggregation();
}

const Aggregation *Manager::defaultAggregation()
{
    KConfigGroup conf(MessageListSettings::self()->config(),
                      MessageList::Util::storageModelAggregationsGroup());

    const QString aggregationId = conf.readEntry(QStringLiteral("DefaultSet"), "");

Laurent Montel's avatar
Laurent Montel committed
113
    Aggregation *opt = nullptr;
114 115 116 117 118 119 120 121 122 123

    if (!aggregationId.isEmpty()) {
        opt = mAggregations.value(aggregationId);
    }

    if (opt) {
        return opt;
    }

    // try just the first one
Laurent Montel's avatar
Laurent Montel committed
124
    QMap< QString, Aggregation * >::ConstIterator it = mAggregations.constBegin();
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
    if (it != mAggregations.constEnd()) {
        return *it;
    }

    // aargh
    createDefaultAggregations();

    return *(mAggregations.constBegin());
}

void Manager::saveAggregationForStorageModel(const Akonadi::Collection &col, const QString &id, bool storageUsesPrivateAggregation)
{
    if (!col.isValid()) {
        return;
    }
    saveAggregationForStorageModel(QString::number(col.id()), id, storageUsesPrivateAggregation);
}

void Manager::saveAggregationForStorageModel(const StorageModel *storageModel, const QString &id, bool storageUsesPrivateAggregation)
{
    saveAggregationForStorageModel(storageModel->id(), id, storageUsesPrivateAggregation);
}

void Manager::saveAggregationForStorageModel(const QString &modelId, const QString &id, bool storageUsesPrivateAggregation)
{
    KConfigGroup conf(MessageListSettings::self()->config(),
                      MessageList::Util::storageModelAggregationsGroup());

    if (storageUsesPrivateAggregation) {
        conf.writeEntry(MessageList::Util::setForStorageModelConfigName().arg(modelId), id);
    } else {
        conf.deleteEntry(MessageList::Util::setForStorageModelConfigName().arg(modelId));
    }

    if (!storageUsesPrivateAggregation) {
        conf.writeEntry(QStringLiteral("DefaultSet"), id);
    }
}

const Aggregation *Manager::aggregationForStorageModel(const Akonadi::Collection &col, bool *storageUsesPrivateAggregation)
{
    Q_ASSERT(storageUsesPrivateAggregation);

    *storageUsesPrivateAggregation = false; // this is by default

    if (!col.isValid()) {
        return defaultAggregation();
    }
    return Manager::aggregationForStorageModel(QString::number(col.id()), storageUsesPrivateAggregation);
}

const Aggregation *Manager::aggregationForStorageModel(const StorageModel *storageModel, bool *storageUsesPrivateAggregation)
{
    Q_ASSERT(storageUsesPrivateAggregation);

    *storageUsesPrivateAggregation = false; // this is by default

    if (!storageModel) {
        return defaultAggregation();
    }
    return Manager::aggregationForStorageModel(storageModel->id(), storageUsesPrivateAggregation);
}

const Aggregation *Manager::aggregationForStorageModel(const QString &storageId, bool *storageUsesPrivateAggregation)
{
    KConfigGroup conf(MessageListSettings::self()->config(),
                      MessageList::Util::storageModelAggregationsGroup());

    const QString aggregationId = conf.readEntry(MessageList::Util::setForStorageModelConfigName().arg(storageId), "");

Laurent Montel's avatar
Laurent Montel committed
195
    Aggregation *opt = nullptr;
196 197 198 199

    if (!aggregationId.isEmpty()) {
        // a private aggregation was stored
        opt = mAggregations.value(aggregationId);
Laurent Montel's avatar
Laurent Montel committed
200
        *storageUsesPrivateAggregation = (opt != nullptr);
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
    }

    if (opt) {
        return opt;
    }

    // FIXME: If the storageModel is a mailing list, maybe suggest a mailing-list like preset...
    //        We could even try to guess if the storageModel is a mailing list

    return defaultAggregation();
}

void Manager::addAggregation(Aggregation *set)
{
    Aggregation *old = mAggregations.value(set->id());
    delete old;
    mAggregations.insert(set->id(), set);
}

void Manager::createDefaultAggregations()
{
    addAggregation(
        new Aggregation(
            i18n("Current Activity, Threaded"),
            i18n("This view uses smart date range groups. " \
                 "Messages are threaded. " \
                 "So for example, in \"Today\" you will find all the messages arrived today " \
                 "and all the threads that have been active today."
Laurent Montel's avatar
Laurent Montel committed
229
                 ),
230 231 232 233 234 235 236
            Aggregation::GroupByDateRange,
            Aggregation::ExpandRecentGroups,
            Aggregation::PerfectReferencesAndSubject,
            Aggregation::MostRecentMessage,
            Aggregation::ExpandThreadsWithUnreadOrImportantMessages,
            Aggregation::FavorInteractivity,
            true
Laurent Montel's avatar
Laurent Montel committed
237 238
            )
        );
239 240 241 242 243 244 245

    addAggregation(
        new Aggregation(
            i18n("Current Activity, Flat"),
            i18n("This view uses smart date range groups. " \
                 "Messages are not threaded. " \
                 "So for example, in \"Today\" you will simply find all the messages arrived today."
Laurent Montel's avatar
Laurent Montel committed
246
                 ),
247 248 249 250 251 252 253
            Aggregation::GroupByDateRange,
            Aggregation::ExpandRecentGroups,
            Aggregation::NoThreading,
            Aggregation::MostRecentMessage,
            Aggregation::NeverExpandThreads,
            Aggregation::FavorInteractivity,
            true
Laurent Montel's avatar
Laurent Montel committed
254 255
            )
        );
256 257 258 259 260 261 262 263

    addAggregation(
        new Aggregation(
            i18n("Activity by Date, Threaded"),
            i18n("This view uses day-by-day groups. " \
                 "Messages are threaded. " \
                 "So for example, in \"Today\" you will find all the messages arrived today " \
                 "and all the threads that have been active today."
Laurent Montel's avatar
Laurent Montel committed
264
                 ),
265 266 267 268 269 270 271
            Aggregation::GroupByDate,
            Aggregation::ExpandRecentGroups,
            Aggregation::PerfectReferencesAndSubject,
            Aggregation::MostRecentMessage,
            Aggregation::ExpandThreadsWithUnreadOrImportantMessages,
            Aggregation::FavorInteractivity,
            true
Laurent Montel's avatar
Laurent Montel committed
272 273
            )
        );
274 275 276 277 278 279 280

    addAggregation(
        new Aggregation(
            i18n("Activity by Date, Flat"),
            i18n("This view uses day-by-day groups. " \
                 "Messages are not threaded. " \
                 "So for example, in \"Today\" you will simply find all the messages arrived today."
Laurent Montel's avatar
Laurent Montel committed
281
                 ),
282 283 284 285 286 287 288
            Aggregation::GroupByDate,
            Aggregation::ExpandRecentGroups,
            Aggregation::NoThreading,
            Aggregation::MostRecentMessage,
            Aggregation::NeverExpandThreads,
            Aggregation::FavorInteractivity,
            true
Laurent Montel's avatar
Laurent Montel committed
289 290
            )
        );
291 292 293 294 295 296 297 298 299 300 301 302

    addAggregation(
        new Aggregation(
            i18n("Standard Mailing List"),
            i18n("This is a plain and old mailing list view: no groups and heavy threading."),
            Aggregation::NoGrouping,
            Aggregation::NeverExpandGroups,
            Aggregation::PerfectReferencesAndSubject,
            Aggregation::TopmostMessage,
            Aggregation::ExpandThreadsWithUnreadOrImportantMessages,
            Aggregation::FavorInteractivity,
            true
Laurent Montel's avatar
Laurent Montel committed
303 304
            )
        );
305 306 307 308 309

    addAggregation(
        new Aggregation(
            i18n("Flat Date View"),
            i18n("This is a plain and old list of messages sorted by date: no groups and no threading." \
Laurent Montel's avatar
Laurent Montel committed
310
                 ),
311 312 313 314 315 316 317 318
            Aggregation::NoGrouping,
            Aggregation::NeverExpandGroups,
            Aggregation::NoThreading,
            Aggregation::TopmostMessage,
            Aggregation::NeverExpandThreads,
            Aggregation::FavorInteractivity,
            true

Laurent Montel's avatar
Laurent Montel committed
319 320
            )
        );
321 322 323 324 325 326 327

    addAggregation(
        new Aggregation(
            i18n("Senders/Receivers, Flat"),
            i18n("This view groups the messages by senders or receivers (depending on the folder " \
                 "type). " \
                 "Messages are not threaded."
Laurent Montel's avatar
Laurent Montel committed
328
                 ),
329 330 331 332 333 334 335
            Aggregation::GroupBySenderOrReceiver,
            Aggregation::NeverExpandGroups,
            Aggregation::NoThreading,
            Aggregation::TopmostMessage,
            Aggregation::NeverExpandThreads,
            Aggregation::FavorSpeed,
            true
Laurent Montel's avatar
Laurent Montel committed
336 337
            )
        );
338 339 340 341 342 343 344 345 346 347 348 349

    addAggregation(
        new Aggregation(
            i18n("Thread Starters"),
            i18n("This view groups the messages in threads and then groups the threads by the starting user."),
            Aggregation::GroupBySenderOrReceiver,
            Aggregation::NeverExpandGroups,
            Aggregation::PerfectReferencesAndSubject,
            Aggregation::TopmostMessage,
            Aggregation::NeverExpandThreads,
            Aggregation::FavorSpeed,
            true
Laurent Montel's avatar
Laurent Montel committed
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

    /*
    FIX THIS
    addAggregation(
      new Aggregation(
          i18n( "Recent Thread Starters" ),
          i18n( "This view groups the messages in threads and then groups the threads by the starting user. " \
                "Groups are sorted by the date of the first thread start. "
            ),
          Aggregation::GroupBySenderOrReceiver,
          Aggregation::SortGroupsByDateTimeOfMostRecent,
          Aggregation::Descending,
          Aggregation::PerfectReferencesAndSubject,
          Aggregation::TopmostMessage,
          Aggregation::SortMessagesByDateTime,
          Aggregation::Descending
        )
    );
    */
}

void Manager::removeAllAggregations()
{
Laurent Montel's avatar
Laurent Montel committed
375 376
    QMap< QString, Aggregation * >::ConstIterator end(mAggregations.constEnd());
    for (QMap< QString, Aggregation * >::ConstIterator it = mAggregations.constBegin(); it != end; ++it) {
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
        delete(*it);
    }

    mAggregations.clear();
}

void Manager::aggregationsConfigurationCompleted()
{
    if (mAggregations.isEmpty()) {
        createDefaultAggregations();    // panic
    }

    saveConfiguration(); // just to be sure :)

    // notify all the widgets that they should reload the option set combos
    Q_EMIT aggregationsChanged();
}

const SortOrder Manager::sortOrderForStorageModel(const StorageModel *storageModel, bool *storageUsesPrivateSortOrder)
{
    Q_ASSERT(storageUsesPrivateSortOrder);

    *storageUsesPrivateSortOrder = false; // this is by default

    if (!storageModel) {
        return SortOrder();
    }

    KConfigGroup conf(MessageListSettings::self()->config(), MessageList::Util::storageModelSortOrderGroup());
    SortOrder ret;
    ret.readConfig(conf, storageModel->id(), storageUsesPrivateSortOrder);
    return ret;
}

Laurent Montel's avatar
Laurent Montel committed
411
void Manager::saveSortOrderForStorageModel(const StorageModel *storageModel, SortOrder order, bool storageUsesPrivateSortOrder)
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
{
    KConfigGroup conf(MessageListSettings::self()->config(), MessageList::Util::storageModelSortOrderGroup());
    order.writeConfig(conf, storageModel->id(), storageUsesPrivateSortOrder);
}

const Theme *Manager::theme(const QString &id)
{
    Theme *opt = mThemes.value(id);
    if (opt) {
        return opt;
    }

    return defaultTheme();
}

const Theme *Manager::defaultTheme()
{
    KConfigGroup conf(MessageListSettings::self()->config(), MessageList::Util::storageModelThemesGroup());

    const QString themeId = conf.readEntry(QStringLiteral("DefaultSet"), "");

Laurent Montel's avatar
Laurent Montel committed
433
    Theme *opt = nullptr;
434 435 436 437 438 439 440 441 442 443

    if (!themeId.isEmpty()) {
        opt = mThemes.value(themeId);
    }

    if (opt) {
        return opt;
    }

    // try just the first one
Laurent Montel's avatar
Laurent Montel committed
444
    QMap< QString, Theme * >::ConstIterator it = mThemes.constBegin();
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    if (it != mThemes.constEnd()) {
        return *it;
    }

    // aargh
    createDefaultThemes();

    it = mThemes.constBegin();

    Q_ASSERT(it != mThemes.constEnd());

    return *it;
}

void Manager::saveThemeForStorageModel(int index, const QString &id, bool storageUsesPrivateTheme)
{
    saveThemeForStorageModel(QString::number(index), id, storageUsesPrivateTheme);
}

void Manager::saveThemeForStorageModel(const StorageModel *storageModel, const QString &id, bool storageUsesPrivateTheme)
{
    saveThemeForStorageModel(storageModel->id(), id, storageUsesPrivateTheme);
}

void Manager::saveThemeForStorageModel(const QString &storageModelIndex, const QString &id, bool storageUsesPrivateTheme)
{
    KConfigGroup conf(MessageListSettings::self()->config(), MessageList::Util::storageModelThemesGroup());

    if (storageUsesPrivateTheme) {
        conf.writeEntry(MessageList::Util::setForStorageModelConfigName().arg(storageModelIndex), id);
    } else {
        conf.deleteEntry(MessageList::Util::setForStorageModelConfigName().arg(storageModelIndex));
    }

    if (!storageUsesPrivateTheme) {
        conf.writeEntry(QStringLiteral("DefaultSet"), id);
    }
}

Laurent Montel's avatar
Laurent Montel committed
484
const Theme *Manager::themeForStorageModel(const Akonadi::Collection &col, bool *storageUsesPrivateTheme)
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
{
    Q_ASSERT(storageUsesPrivateTheme);

    *storageUsesPrivateTheme = false; // this is by default

    if (!col.isValid()) {
        return defaultTheme();
    }
    return Manager::themeForStorageModel(QString::number(col.id()), storageUsesPrivateTheme);
}

const Theme *Manager::themeForStorageModel(const StorageModel *storageModel, bool *storageUsesPrivateTheme)
{
    Q_ASSERT(storageUsesPrivateTheme);

    *storageUsesPrivateTheme = false; // this is by default

    if (!storageModel) {
        return defaultTheme();
    }
    return Manager::themeForStorageModel(storageModel->id(), storageUsesPrivateTheme);
}

const Theme *Manager::themeForStorageModel(const QString &id, bool *storageUsesPrivateTheme)
{
    KConfigGroup conf(MessageListSettings::self()->config(), MessageList::Util::storageModelThemesGroup());
    const QString themeId = conf.readEntry(MessageList::Util::setForStorageModelConfigName().arg(id), "");

Laurent Montel's avatar
Laurent Montel committed
513
    Theme *opt = nullptr;
514 515 516 517

    if (!themeId.isEmpty()) {
        // a private theme was stored
        opt = mThemes.value(themeId);
Laurent Montel's avatar
Laurent Montel committed
518
        *storageUsesPrivateTheme = (opt != nullptr);
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    }

    if (opt) {
        return opt;
    }

    // FIXME: If the storageModel is a mailing list, maybe suggest a mailing-list like preset...
    //        We could even try to guess if the storageModel is a mailing list

    // FIXME: Prefer right-to-left themes when application layout is RTL.

    return defaultTheme();
}

void Manager::addTheme(Theme *set)
{
    Theme *old = mThemes.value(set->id());
    delete old;
    mThemes.insert(set->id(), set);
}

Laurent Montel's avatar
Laurent Montel committed
540
static Theme::Column *add_theme_simple_text_column(Theme *s, const QString &name, Theme::ContentItem::Type type, bool visibleByDefault, SortOrder::MessageSorting messageSorting, bool alignRight, bool addGroupHeaderItem)
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
{
    Theme::Column *c = new Theme::Column();
    c->setLabel(name);
    c->setVisibleByDefault(visibleByDefault);
    c->setMessageSorting(messageSorting);

    Theme::Row *r = new Theme::Row();

    Theme::ContentItem *i = new Theme::ContentItem(type);

    if (alignRight) {
        r->addRightItem(i);
    } else {
        r->addLeftItem(i);
    }

    c->addMessageRow(r);

    if (addGroupHeaderItem) {
        Theme::Row *r = new Theme::Row();

        Theme::ContentItem *i = new Theme::ContentItem(type);

        if (alignRight) {
            r->addRightItem(i);
        } else {
            r->addLeftItem(i);
        }

        c->addGroupHeaderRow(r);
    }

    s->addColumn(c);

    return c;
}

Laurent Montel's avatar
Laurent Montel committed
578
static Theme::Column *add_theme_simple_icon_column(Theme *s, const QString &name, const QString &pixmapName, Theme::ContentItem::Type type, bool visibleByDefault, SortOrder::MessageSorting messageSorting)
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
{
    Theme::Column *c = new Theme::Column();
    c->setLabel(name);
    c->setPixmapName(pixmapName);
    c->setVisibleByDefault(visibleByDefault);
    c->setMessageSorting(messageSorting);

    Theme::Row *r = new Theme::Row();

    Theme::ContentItem *i = new Theme::ContentItem(type);
    i->setSoftenByBlendingWhenDisabled(true);

    r->addLeftItem(i);

    c->addMessageRow(r);

    s->addColumn(c);

    return c;
}

void Manager::createDefaultThemes()
{
    Theme *s;
    Theme::Column *c;
    Theme::Row *r;
    Theme::ContentItem *i;

    // The "Classic" backward compatible theme

    s = new Theme(
        i18nc("Default theme name", "Classic"),
        i18n("A simple, backward compatible, single row theme"), true   /*readOnly*/
Laurent Montel's avatar
Laurent Montel committed
612
        );
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656

    c = new Theme::Column();
    c->setLabel(i18nc("@title:column Subject of messages", "Subject"));
    c->setMessageSorting(SortOrder::SortMessagesBySubject);

    r = new Theme::Row();
    i = new Theme::ContentItem(Theme::ContentItem::ExpandedStateIcon);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::GroupHeaderLabel);
    i->setBold(true);
    r->addLeftItem(i);
    c->addGroupHeaderRow(r);

    r = new Theme::Row();
    i = new Theme::ContentItem(Theme::ContentItem::CombinedReadRepliedStateIcon);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::AttachmentStateIcon);
    i->setHideWhenDisabled(true);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::AnnotationIcon);
    i->setHideWhenDisabled(true);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::InvitationIcon);
    i->setHideWhenDisabled(true);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::SignatureStateIcon);
    i->setHideWhenDisabled(true);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::EncryptionStateIcon);
    i->setHideWhenDisabled(true);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::Subject);
    r->addLeftItem(i);
    c->addMessageRow(r);

    s->addColumn(c);

    c = add_theme_simple_text_column(s, i18n("Sender/Receiver"), Theme::ContentItem::SenderOrReceiver, true, SortOrder::SortMessagesBySenderOrReceiver, false, false);
    c->setIsSenderOrReceiver(true);
    add_theme_simple_text_column(s, i18nc("Sender of a message", "Sender"), Theme::ContentItem::Sender, false, SortOrder::SortMessagesBySender, false, false);
    add_theme_simple_text_column(s, i18nc("Receiver of a message", "Receiver"), Theme::ContentItem::Receiver, false, SortOrder::SortMessagesByReceiver, false, false);
    add_theme_simple_text_column(s, i18nc("Date of a message", "Date"), Theme::ContentItem::Date, true, SortOrder::SortMessagesByDateTime, false, false);
    add_theme_simple_text_column(s, i18n("Most Recent Date"), Theme::ContentItem::MostRecentDate, false, SortOrder::SortMessagesByDateTimeOfMostRecent, false, true);
    add_theme_simple_text_column(s, i18nc("Size of a message", "Size"), Theme::ContentItem::Size, false, SortOrder::SortMessagesBySize, false, false);
Sandro Knauß's avatar
Sandro Knauß committed
657
    add_theme_simple_icon_column(s, i18nc("Attachment indication", "Attachment"), QStringLiteral(
Laurent Montel's avatar
Laurent Montel committed
658
                                     "mail-attachment"), Theme::ContentItem::AttachmentStateIcon, false, SortOrder::SortMessagesByAttachmentStatus);
Laurent Montel's avatar
Laurent Montel committed
659
    add_theme_simple_icon_column(s, i18n("Read/Unread"), QStringLiteral("mail-mark-unread-new"), Theme::ContentItem::ReadStateIcon, false, SortOrder::SortMessagesByUnreadStatus);
660
    add_theme_simple_icon_column(s, i18n("Replied"), QStringLiteral("mail-replied"), Theme::ContentItem::RepliedStateIcon, false, SortOrder::NoMessageSorting);
Laurent Montel's avatar
Laurent Montel committed
661
    add_theme_simple_icon_column(s, i18nc("Message importance indication", "Important"), QStringLiteral(
662
                                     "mail-mark-important"), Theme::ContentItem::ImportantStateIcon, false, SortOrder::SortMessagesByImportantStatus);
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    add_theme_simple_icon_column(s, i18n("Action Item"), QStringLiteral("mail-task"), Theme::ContentItem::ActionItemStateIcon, false, SortOrder::SortMessagesByActionItemStatus);
    add_theme_simple_icon_column(s, i18n("Spam/Ham"), QStringLiteral("mail-mark-junk"), Theme::ContentItem::SpamHamStateIcon, false, SortOrder::NoMessageSorting);
    add_theme_simple_icon_column(s, i18n("Watched/Ignored"), QStringLiteral("mail-thread-watch"), Theme::ContentItem::WatchedIgnoredStateIcon, false, SortOrder::NoMessageSorting);
    add_theme_simple_icon_column(s, i18n("Encryption"), QStringLiteral("mail-encrypted-full"), Theme::ContentItem::EncryptionStateIcon, false, SortOrder::NoMessageSorting);
    add_theme_simple_icon_column(s, i18n("Signature"), QStringLiteral("mail-signed-verified"), Theme::ContentItem::SignatureStateIcon, false, SortOrder::NoMessageSorting);
    add_theme_simple_icon_column(s, i18n("Tag List"), QStringLiteral("feed-subscribe"), Theme::ContentItem::TagList, false, SortOrder::NoMessageSorting);

    s->resetColumnState(); // so it's initially set from defaults

    addTheme(s);

    // The Fancy theme

    s = new Theme(
        i18n("Smart"),
        i18n("A smart multiline and multi item theme"), true   /*readOnly*/
Laurent Montel's avatar
Laurent Montel committed
679
        );
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800

    c = new Theme::Column();
    c->setLabel(i18n("Message"));

    r = new Theme::Row();
    i = new Theme::ContentItem(Theme::ContentItem::ExpandedStateIcon);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::GroupHeaderLabel);
    i->setBold(true);
    r->addLeftItem(i);
    c->addGroupHeaderRow(r);

    r = new Theme::Row();
    i = new Theme::ContentItem(Theme::ContentItem::Subject);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::ReadStateIcon);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::RepliedStateIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::AttachmentStateIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::AnnotationIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::InvitationIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::EncryptionStateIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::SignatureStateIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::TagList);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    c->addMessageRow(r);

    Theme::Row *firstFancyRow = r;  // save it so we can continue adding stuff below (after cloning the theme)

    r = new Theme::Row();
    i = new Theme::ContentItem(Theme::ContentItem::SenderOrReceiver);
    i->setSoftenByBlending(true);
    i->setItalic(true);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::Date);
    i->setSoftenByBlending(true);
    i->setItalic(true);
    r->addRightItem(i);
    c->addMessageRow(r);

    s->addColumn(c);

    // clone the "Fancy theme" here so we'll use it as starting point for the "Fancy with clickable status"
    Theme *fancyWithClickableStatus = new Theme(*s);
    fancyWithClickableStatus->detach();
    fancyWithClickableStatus->generateUniqueId();

    // and continue the "Fancy" specific settings
    r = firstFancyRow;

    i = new Theme::ContentItem(Theme::ContentItem::ActionItemStateIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::ImportantStateIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::SpamHamStateIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::WatchedIgnoredStateIcon);
    i->setHideWhenDisabled(true);
    r->addRightItem(i);

    s->setViewHeaderPolicy(Theme::NeverShowHeader);

    s->resetColumnState(); // so it's initially set from defaults

    addTheme(s);

    // The "Fancy with Clickable Status" theme

    s = fancyWithClickableStatus;

    s->setName(i18n("Smart with Clickable Status"));
    s->setDescription(i18n("A smart multiline and multi item theme with a clickable status column"));
    s->setReadOnly(true);

    c = new Theme::Column();
    c->setLabel(i18n("Status"));
    c->setVisibleByDefault(true);

    r = new Theme::Row();
    i = new Theme::ContentItem(Theme::ContentItem::ActionItemStateIcon);
    i->setSoftenByBlendingWhenDisabled(true);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::ImportantStateIcon);
    i->setSoftenByBlendingWhenDisabled(true);
    r->addLeftItem(i);
    c->addMessageRow(r);

    r = new Theme::Row();
    i = new Theme::ContentItem(Theme::ContentItem::SpamHamStateIcon);
    i->setSoftenByBlendingWhenDisabled(true);
    r->addLeftItem(i);
    i = new Theme::ContentItem(Theme::ContentItem::WatchedIgnoredStateIcon);
    i->setSoftenByBlendingWhenDisabled(true);
    r->addLeftItem(i);
    c->addMessageRow(r);

    s->addColumn(c);

    s->resetColumnState(); // so it's initially set from defaults

    addTheme(s);
}

void Manager::removeAllThemes()
{
Laurent Montel's avatar
Laurent Montel committed
801 802
    QMap< QString, Theme * >::ConstIterator end(mThemes.constEnd());
    for (QMap< QString, Theme * >::ConstIterator it = mThemes.constBegin(); it != end; ++it) {
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
        delete(*it);
    }

    mThemes.clear();
}

void Manager::themesConfigurationCompleted()
{
    if (mThemes.isEmpty()) {
        createDefaultThemes();    // panic
    }

    saveConfiguration(); // just to be sure :)

    // notify all the widgets that they should reload the option set combos
    Q_EMIT themesChanged();
}

void Manager::reloadAllWidgets()
{
    QList< Widget * >::ConstIterator end(mWidgetList.constEnd());
    for (QList< Widget * >::ConstIterator it = mWidgetList.constBegin(); it != end; ++it) {
        if ((*it)->view()) {
            (*it)->view()->reload();
        }
    }
}

void Manager::reloadGlobalConfiguration()
{
    // This is called when configuration changes (probably edited by the options dialog)
    const int oldDateFormat = (int)mDateFormatter->format();
    const QString oldDateCustomFormat = mDateFormatter->customFormat();

    loadGlobalConfiguration();

    if (
Laurent Montel's avatar
Laurent Montel committed
840 841 842
        (oldDateFormat != (int)mDateFormatter->format())
        || (oldDateCustomFormat != mDateFormatter->customFormat())
        ) {
843 844 845 846 847 848 849 850
        reloadAllWidgets();
    }
}

void Manager::loadGlobalConfiguration()
{
    // Load the date format
    const KMime::DateFormatter::FormatType type = static_cast<KMime::DateFormatter::FormatType>(
Laurent Montel's avatar
Laurent Montel committed
851
        MessageCore::MessageCoreSettings::self()->dateFormat());
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    mDateFormatter->setCustomFormat(MessageCore::MessageCoreSettings::self()->customDateFormat());
    mDateFormatter->setFormat(type);
}

void Manager::loadConfiguration()
{
    loadGlobalConfiguration();

    {
        // load Aggregations

        KConfigGroup conf(MessageListSettings::self()->config(), "MessageListView::Aggregations");

        mAggregations.clear();

        const int cnt = conf.readEntry("Count", 0);

        int idx = 0;
        while (idx < cnt) {
            const QString data = conf.readEntry(QStringLiteral("Set%1").arg(idx), QString());
            if (!data.isEmpty()) {
                Aggregation *set = new Aggregation();
                if (set->loadFromString(data)) {
                    if (Aggregation *old = mAggregations.value(set->id())) {
                        delete old;
                    }
                    mAggregations.insert(set->id(), set);
                } else {
                    delete set; // b0rken
                }
            }
            idx++;
        }

        if (mAggregations.isEmpty()) {
            // don't allow zero configuration, create some presets
            createDefaultAggregations();
        }
    }

    {
        // load Themes

        KConfigGroup conf(MessageListSettings::self()->config(), "MessageListView::Themes");

        mThemes.clear();

        const int cnt = conf.readEntry("Count", 0);

        int idx = 0;
        while (idx < cnt) {
            const QString data = conf.readEntry(QStringLiteral("Set%1").arg(idx), QString());
            if (!data.isEmpty()) {
                Theme *set = new Theme();
                if (set->loadFromString(data)) {
                    if (Theme *old = mThemes.value(set->id())) {
                        delete old;
                    }
                    mThemes.insert(set->id(), set);
                } else {
                    qCWarning(MESSAGELIST_LOG) << "Saved theme loading failed";
                    delete set; // b0rken
                }
            }
            ++idx;
        }

        if (mThemes.isEmpty()) {
            // don't allow zero configuration, create some presets
            createDefaultThemes();
        }
    }
}

void Manager::saveGlobalConfiguration()
{
    MessageListSettings::self()->save();
}

void Manager::saveConfiguration()
{
    saveGlobalConfiguration();

    {
        // store aggregations

        KConfigGroup conf(MessageListSettings::self()->config(), "MessageListView::Aggregations");
        //conf.clear();

        conf.writeEntry("Count", mAggregations.count());

        int idx = 0;
Laurent Montel's avatar
Laurent Montel committed
944 945
        QMap< QString, Aggregation * >::ConstIterator end(mAggregations.end());
        for (QMap< QString, Aggregation * >::ConstIterator it = mAggregations.constBegin(); it != end; ++it) {
946 947 948 949 950 951 952 953 954 955 956 957 958 959
            conf.writeEntry(QStringLiteral("Set%1").arg(idx), (*it)->saveToString());
            ++idx;
        }
    }

    {
        // store themes

        KConfigGroup conf(MessageListSettings::self()->config(), "MessageListView::Themes");
        //conf.clear();

        conf.writeEntry("Count", mThemes.count());

        int idx = 0;
Laurent Montel's avatar
Laurent Montel committed
960 961
        QMap< QString, Theme * >::ConstIterator end(mThemes.constEnd());
        for (QMap< QString, Theme * >::ConstIterator it = mThemes.constBegin(); it != end; ++it) {
962 963 964 965 966 967 968
            conf.writeEntry(QStringLiteral("Set%1").arg(idx), (*it)->saveToString());
            ++idx;
        }
    }

    MessageListSettings::self()->config()->sync();
}