File indexing completed on 2025-01-26 05:24:12
0001 /* 0002 This file is part of the Okteta Kasten module, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2009-2010, 2012 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "modifiedbarcontroller.hpp" 0010 0011 // controller 0012 #include "iconlabel.hpp" 0013 // Kasten ui 0014 #include <Kasten/StatusBar> 0015 // Kasten core 0016 #include <Kasten/AbstractModelSynchronizer> 0017 #include <Kasten/AbstractDocument> 0018 // KF 0019 #include <KLocalizedString> 0020 // Qt 0021 #include <QIcon> 0022 0023 namespace Kasten { 0024 ModifiedBarController::ModifiedBarController(StatusBar* statusBar) 0025 { 0026 mLocalStateLabel = new IconLabel(statusBar); 0027 statusBar->addWidget(mLocalStateLabel); 0028 0029 mRemoteStateLabel = new IconLabel(statusBar); 0030 statusBar->addWidget(mRemoteStateLabel); 0031 0032 setTargetModel(nullptr); 0033 } 0034 0035 void ModifiedBarController::setTargetModel(AbstractModel* model) 0036 { 0037 AbstractDocument* newDocument = model ? model->findBaseModel<AbstractDocument*>() : nullptr; 0038 0039 if (mDocument == newDocument) { 0040 return; 0041 } 0042 0043 if (mDocument) { 0044 mDocument->disconnect(this); 0045 } 0046 0047 mDocument = newDocument; 0048 0049 if (mDocument) { 0050 connect(mDocument, &Kasten::AbstractDocument::synchronizerChanged, 0051 this, &ModifiedBarController::onSynchronizerChanged); 0052 } 0053 0054 mLocalStateLabel->setEnabled(mDocument != nullptr); 0055 mRemoteStateLabel->setEnabled(mDocument != nullptr); 0056 0057 onSynchronizerChanged(mDocument ? mDocument->synchronizer() : nullptr); 0058 } 0059 0060 void ModifiedBarController::onContentFlagsChanged(ContentFlags contentFlags) 0061 { 0062 const bool hasChanges = (contentFlags & ContentHasUnstoredChanges); 0063 onLocalSyncStateChanged(hasChanges ? LocalHasChanges : LocalInSync); 0064 } 0065 0066 void ModifiedBarController::onLocalSyncStateChanged(LocalSyncState localSyncState) 0067 { 0068 const bool isModified = (localSyncState == LocalHasChanges); 0069 0070 const QIcon icon = isModified ? QIcon::fromTheme(QStringLiteral("document-save")) : QIcon(); 0071 mLocalStateLabel->setIcon(icon); 0072 0073 mLocalStateLabel->setToolTip(isModified ? 0074 i18nc("@tooltip the document is modified", "Modified.") : 0075 i18nc("@tooltip the document is not modified", "Not modified.")); 0076 0077 } 0078 0079 void ModifiedBarController::onRemoteSyncStateChanged(RemoteSyncState remoteSyncState) 0080 { 0081 const char* const iconName = 0082 (!mSynchronizer) ? "document-new" : 0083 (remoteSyncState == RemoteHasChanges) ? "document-save" : 0084 (remoteSyncState == RemoteDeleted) ? "edit-delete" : 0085 (remoteSyncState == RemoteUnknown) ? "flag-yellow" : 0086 (remoteSyncState == RemoteUnreachable) ? "network-disconnect" : 0087 /* else */ nullptr; 0088 0089 const QIcon icon = iconName ? QIcon::fromTheme(QLatin1String(iconName)) : QIcon(); 0090 mRemoteStateLabel->setIcon(icon); 0091 0092 // TODO: tooltips 0093 } 0094 0095 void ModifiedBarController::onSynchronizerChanged(AbstractModelSynchronizer* newSynchronizer) 0096 { 0097 if (mSynchronizer) { 0098 mSynchronizer->disconnect(this); 0099 } 0100 0101 AbstractModelSynchronizer* oldSynchronizer = mSynchronizer; 0102 mSynchronizer = newSynchronizer; 0103 0104 LocalSyncState localState; 0105 RemoteSyncState remoteState; 0106 if (mSynchronizer) { 0107 if (!oldSynchronizer) { 0108 disconnect(mDocument, &Kasten::AbstractDocument::contentFlagsChanged, 0109 this, &ModifiedBarController::onContentFlagsChanged); 0110 } 0111 0112 localState = mSynchronizer->localSyncState(); 0113 remoteState = mSynchronizer->remoteSyncState(); 0114 0115 connect(mSynchronizer, &Kasten::AbstractModelSynchronizer::localSyncStateChanged, 0116 this, &ModifiedBarController::onLocalSyncStateChanged); 0117 connect(mSynchronizer, &Kasten::AbstractModelSynchronizer::remoteSyncStateChanged, 0118 this, &ModifiedBarController::onRemoteSyncStateChanged); 0119 connect(mSynchronizer, &QObject::destroyed, 0120 this, &ModifiedBarController::onSynchronizerDeleted); 0121 } else if (mDocument) { 0122 const bool hasChanges = (mDocument->contentFlags() & ContentHasUnstoredChanges); 0123 localState = (hasChanges ? LocalHasChanges : LocalInSync); 0124 // TODO: onRemoteSyncStateChanged(...) checks for mSynchronizer and ignores this 0125 remoteState = RemoteInSync; 0126 0127 connect(mDocument, &Kasten::AbstractDocument::contentFlagsChanged, 0128 this, &ModifiedBarController::onContentFlagsChanged); 0129 } else { 0130 localState = LocalInSync; 0131 // TODO: onRemoteSyncStateChanged(...) checks for mSynchronizer and ignores this 0132 remoteState = RemoteInSync; 0133 } 0134 0135 onLocalSyncStateChanged(localState); 0136 onRemoteSyncStateChanged(remoteState); 0137 } 0138 0139 void ModifiedBarController::onSynchronizerDeleted(QObject* synchronizer) 0140 { 0141 if (synchronizer != mSynchronizer) { 0142 return; 0143 } 0144 0145 mSynchronizer = nullptr; 0146 0147 // switch to document state 0148 connect(mDocument, &Kasten::AbstractDocument::contentFlagsChanged, 0149 this, &ModifiedBarController::onContentFlagsChanged); 0150 0151 onContentFlagsChanged(mDocument->contentFlags()); 0152 // TODO: onRemoteSyncStateChanged(...) checks for mSynchronizer and ignores the parameter 0153 onRemoteSyncStateChanged(RemoteInSync); 0154 } 0155 0156 } 0157 0158 #include "moc_modifiedbarcontroller.cpp"