forked from qt-creator/qt-creator
CPaster: Aspectify FileShareProtocolSettings
Change-Id: I9ae9975eb6fd2c4124d17e2862afa9cde32e9438 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -14,7 +14,6 @@ add_qtc_plugin(CodePaster
|
||||
dpastedotcomprotocol.cpp dpastedotcomprotocol.h
|
||||
fileshareprotocol.cpp fileshareprotocol.h
|
||||
fileshareprotocolsettingspage.cpp fileshareprotocolsettingspage.h
|
||||
fileshareprotocolsettingswidget.ui
|
||||
frontend/argumentscollector.cpp frontend/argumentscollector.h
|
||||
frontend/main.cpp
|
||||
pastebindotcomprotocol.cpp pastebindotcomprotocol.h
|
||||
|
@@ -31,8 +31,7 @@ SOURCES += cpasterplugin.cpp \
|
||||
FORMS += \
|
||||
pasteselect.ui \
|
||||
pasteview.ui \
|
||||
pastebindotcomsettings.ui \
|
||||
fileshareprotocolsettingswidget.ui
|
||||
pastebindotcomsettings.ui
|
||||
include(../../shared/cpaster/cpaster.pri)
|
||||
|
||||
RESOURCES += \
|
||||
|
@@ -25,7 +25,6 @@ QtcPlugin {
|
||||
"fileshareprotocol.h",
|
||||
"fileshareprotocolsettingspage.cpp",
|
||||
"fileshareprotocolsettingspage.h",
|
||||
"fileshareprotocolsettingswidget.ui",
|
||||
"pastebindotcomprotocol.cpp",
|
||||
"pastebindotcomprotocol.h",
|
||||
"pastebindotcomsettings.ui",
|
||||
|
@@ -48,10 +48,9 @@ static const char textElementC[] = "text";
|
||||
namespace CodePaster {
|
||||
|
||||
FileShareProtocol::FileShareProtocol() :
|
||||
m_settings(new FileShareProtocolSettings),
|
||||
m_settingsPage(new FileShareProtocolSettingsPage(m_settings))
|
||||
m_settingsPage(new FileShareProtocolSettingsPage(&m_settings))
|
||||
{
|
||||
m_settings->fromSettings(Core::ICore::settings());
|
||||
m_settings.readSettings(Core::ICore::settings());
|
||||
}
|
||||
|
||||
FileShareProtocol::~FileShareProtocol()
|
||||
@@ -127,7 +126,7 @@ static bool parse(const QString &fileName,
|
||||
|
||||
bool FileShareProtocol::checkConfiguration(QString *errorMessage)
|
||||
{
|
||||
if (m_settings->path.isEmpty()) {
|
||||
if (m_settings.path.value().isEmpty()) {
|
||||
if (errorMessage)
|
||||
*errorMessage = tr("Please configure a path.");
|
||||
return false;
|
||||
@@ -140,7 +139,7 @@ void FileShareProtocol::fetch(const QString &id)
|
||||
// Absolute or relative path name.
|
||||
QFileInfo fi(id);
|
||||
if (fi.isRelative())
|
||||
fi = QFileInfo(m_settings->path + QLatin1Char('/') + id);
|
||||
fi = QFileInfo(m_settings.path.value() + '/' + id);
|
||||
QString errorMessage;
|
||||
QString text;
|
||||
if (parse(fi.absoluteFilePath(), &errorMessage, nullptr, nullptr, &text))
|
||||
@@ -152,7 +151,7 @@ void FileShareProtocol::fetch(const QString &id)
|
||||
void FileShareProtocol::list()
|
||||
{
|
||||
// Read out directory, display by date (latest first)
|
||||
QDir dir(m_settings->path, QLatin1String(tempGlobPatternC),
|
||||
QDir dir(m_settings.path.value(), tempGlobPatternC,
|
||||
QDir::Time, QDir::Files|QDir::NoDotAndDotDot|QDir::Readable);
|
||||
QStringList entries;
|
||||
QString user;
|
||||
@@ -160,7 +159,7 @@ void FileShareProtocol::list()
|
||||
QString errorMessage;
|
||||
const QChar blank = QLatin1Char(' ');
|
||||
const QFileInfoList entryInfoList = dir.entryInfoList();
|
||||
const int count = qMin(m_settings->displayCount, entryInfoList.size());
|
||||
const int count = qMin(int(m_settings.displayCount.value()), entryInfoList.size());
|
||||
for (int i = 0; i < count; i++) {
|
||||
const QFileInfo& entryFi = entryInfoList.at(i);
|
||||
if (parse(entryFi.absoluteFilePath(), &errorMessage, &user, &description)) {
|
||||
@@ -188,7 +187,7 @@ void FileShareProtocol::paste(
|
||||
)
|
||||
{
|
||||
// Write out temp XML file
|
||||
Utils::TempFileSaver saver(m_settings->path + QLatin1Char('/') + QLatin1String(tempPatternC));
|
||||
Utils::TempFileSaver saver(m_settings.path.value() + '/' + tempPatternC);
|
||||
saver.setAutoRemove(false);
|
||||
if (!saver.hasError()) {
|
||||
// Flat text sections embedded into pasterElement
|
||||
|
@@ -26,13 +26,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
#include <QSharedPointer>
|
||||
#include "fileshareprotocolsettingspage.h"
|
||||
|
||||
namespace CodePaster {
|
||||
|
||||
class FileShareProtocolSettingsPage;
|
||||
class FileShareProtocolSettings;
|
||||
|
||||
/* FileShareProtocol: Allows for pasting via a shared network
|
||||
* drive by writing XML files. */
|
||||
@@ -60,7 +58,7 @@ public:
|
||||
const QString &description = QString()) override;
|
||||
|
||||
private:
|
||||
const QSharedPointer<FileShareProtocolSettings> m_settings;
|
||||
FileShareProtocolSettings m_settings;
|
||||
FileShareProtocolSettingsPage *m_settingsPage;
|
||||
};
|
||||
|
||||
|
@@ -28,93 +28,76 @@
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
|
||||
#include <utils/layoutbuilder.h>
|
||||
#include <utils/temporarydirectory.h>
|
||||
|
||||
#include <QSettings>
|
||||
#include <QCoreApplication>
|
||||
|
||||
static const char settingsGroupC[] = "FileSharePasterSettings";
|
||||
static const char pathKeyC[] = "Path";
|
||||
static const char displayCountKeyC[] = "DisplayCount";
|
||||
using namespace Utils;
|
||||
|
||||
namespace CodePaster {
|
||||
|
||||
FileShareProtocolSettings::FileShareProtocolSettings() :
|
||||
path(Utils::TemporaryDirectory::masterDirectoryPath()), displayCount(10)
|
||||
FileShareProtocolSettings::FileShareProtocolSettings()
|
||||
{
|
||||
setSettingsGroup("FileSharePasterSettings");
|
||||
setAutoApply(false);
|
||||
|
||||
registerAspect(&path);
|
||||
path.setSettingsKey("Path");
|
||||
path.setDisplayStyle(StringAspect::PathChooserDisplay);
|
||||
path.setExpectedKind(PathChooser::ExistingDirectory);
|
||||
path.setDefaultValue(TemporaryDirectory::masterDirectoryPath());
|
||||
path.setLabelText(tr("&Path:"));
|
||||
|
||||
registerAspect(&displayCount);
|
||||
displayCount.setSettingsKey("DisplayCount");
|
||||
displayCount.setDefaultValue(10);
|
||||
displayCount.setSuffix(' ' + tr("entries"));
|
||||
displayCount.setLabelText(tr("&Display:"));
|
||||
}
|
||||
|
||||
void FileShareProtocolSettings::toSettings(QSettings *s) const
|
||||
// Settings page
|
||||
|
||||
class FileShareProtocolSettingsWidget final : public Core::IOptionsPageWidget
|
||||
{
|
||||
s->beginGroup(QLatin1String(settingsGroupC));
|
||||
s->setValue(QLatin1String(pathKeyC), path);
|
||||
s->setValue(QLatin1String(displayCountKeyC), displayCount);
|
||||
s->endGroup();
|
||||
}
|
||||
public:
|
||||
FileShareProtocolSettingsWidget(FileShareProtocolSettings *settings)
|
||||
: m_settings(settings)
|
||||
{
|
||||
FileShareProtocolSettings &s = *settings;
|
||||
using namespace Layouting;
|
||||
|
||||
void FileShareProtocolSettings::fromSettings(const QSettings *s)
|
||||
{
|
||||
FileShareProtocolSettings defaultValues;
|
||||
const QString keyRoot = QLatin1String(settingsGroupC) + QLatin1Char('/');
|
||||
path = s->value(keyRoot + QLatin1String(pathKeyC), defaultValues.path).toString();
|
||||
displayCount = s->value(keyRoot + QLatin1String(displayCountKeyC), defaultValues.displayCount).toInt();
|
||||
}
|
||||
auto label = new QLabel(tr("The fileshare-based paster protocol allows for sharing code"
|
||||
"snippets using simple files on a shared network drive. "
|
||||
"Files are never deleted."));
|
||||
label->setWordWrap(true);
|
||||
|
||||
bool FileShareProtocolSettings::equals(const FileShareProtocolSettings &rhs) const
|
||||
{
|
||||
return displayCount == rhs.displayCount && path == rhs.path;
|
||||
}
|
||||
Column {
|
||||
Form {
|
||||
label, Break(),
|
||||
s.path,
|
||||
s.displayCount
|
||||
},
|
||||
Stretch()
|
||||
}.attachTo(this);
|
||||
}
|
||||
|
||||
FileShareProtocolSettingsWidget::FileShareProtocolSettingsWidget()
|
||||
{
|
||||
m_ui.setupUi(this);
|
||||
void apply() final
|
||||
{
|
||||
if (m_settings->isDirty()) {
|
||||
m_settings->apply();
|
||||
m_settings->writeSettings(Core::ICore::settings());
|
||||
}
|
||||
}
|
||||
|
||||
// Add a space in front of the suffix
|
||||
QString suffix = m_ui.displayCountSpinBox->suffix();
|
||||
suffix.prepend(QLatin1Char(' '));
|
||||
m_ui.displayCountSpinBox->setSuffix(suffix);
|
||||
}
|
||||
private:
|
||||
FileShareProtocolSettings *m_settings;
|
||||
};
|
||||
|
||||
void FileShareProtocolSettingsWidget::setSettings(const FileShareProtocolSettings &s)
|
||||
{
|
||||
m_ui.pathChooser->setPath(s.path);
|
||||
m_ui.displayCountSpinBox->setValue(s.displayCount);
|
||||
}
|
||||
|
||||
FileShareProtocolSettings FileShareProtocolSettingsWidget::settings() const
|
||||
{
|
||||
FileShareProtocolSettings rc;
|
||||
rc.path = m_ui.pathChooser->filePath().toString();
|
||||
rc.displayCount = m_ui.displayCountSpinBox->value();
|
||||
return rc;
|
||||
}
|
||||
|
||||
// ----------FileShareProtocolSettingsPage
|
||||
FileShareProtocolSettingsPage::FileShareProtocolSettingsPage(const QSharedPointer<FileShareProtocolSettings> &s)
|
||||
: m_settings(s), m_widget(nullptr)
|
||||
FileShareProtocolSettingsPage::FileShareProtocolSettingsPage(FileShareProtocolSettings *s)
|
||||
{
|
||||
setId("X.CodePaster.FileSharePaster");
|
||||
setDisplayName(tr("Fileshare"));
|
||||
setDisplayName(FileShareProtocolSettingsWidget::tr("Fileshare"));
|
||||
setCategory(Constants::CPASTER_SETTINGS_CATEGORY);
|
||||
setWidgetCreator([s] { return new FileShareProtocolSettingsWidget(s); });
|
||||
}
|
||||
|
||||
QWidget *FileShareProtocolSettingsPage::widget()
|
||||
{
|
||||
if (!m_widget) {
|
||||
m_widget = new FileShareProtocolSettingsWidget;
|
||||
m_widget->setSettings(*m_settings);
|
||||
}
|
||||
return m_widget;
|
||||
}
|
||||
|
||||
void FileShareProtocolSettingsPage::apply()
|
||||
{
|
||||
if (!m_widget) // page was never shown
|
||||
return;
|
||||
const FileShareProtocolSettings newSettings = m_widget->settings();
|
||||
if (newSettings != *m_settings) {
|
||||
*m_settings = newSettings;
|
||||
m_settings->toSettings(Core::ICore::settings());
|
||||
}
|
||||
}
|
||||
} // namespace CodePaster
|
||||
|
@@ -26,61 +26,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <coreplugin/dialogs/ioptionspage.h>
|
||||
#include "ui_fileshareprotocolsettingswidget.h"
|
||||
|
||||
#include <QSharedPointer>
|
||||
#include <QPointer>
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QSettings;
|
||||
QT_END_NAMESPACE
|
||||
#include <utils/aspects.h>
|
||||
|
||||
namespace CodePaster {
|
||||
|
||||
class FileShareProtocolSettings {
|
||||
class FileShareProtocolSettings : public Utils::AspectContainer
|
||||
{
|
||||
Q_DECLARE_TR_FUNCTIONS(CodePaster::FileShareProtocolSettings)
|
||||
|
||||
public:
|
||||
FileShareProtocolSettings();
|
||||
void toSettings(QSettings *) const;
|
||||
void fromSettings(const QSettings *);
|
||||
bool equals(const FileShareProtocolSettings &rhs) const;
|
||||
|
||||
QString path;
|
||||
int displayCount;
|
||||
Utils::StringAspect path;
|
||||
Utils::IntegerAspect displayCount;
|
||||
};
|
||||
|
||||
inline bool operator==(const FileShareProtocolSettings &s1, const FileShareProtocolSettings &s2)
|
||||
{ return s1.equals(s2); }
|
||||
inline bool operator!=(const FileShareProtocolSettings &s1, const FileShareProtocolSettings &s2)
|
||||
{ return !s1.equals(s2); }
|
||||
|
||||
class FileShareProtocolSettingsWidget : public QWidget
|
||||
class FileShareProtocolSettingsPage final : public Core::IOptionsPage
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FileShareProtocolSettingsWidget();
|
||||
|
||||
void setSettings(const FileShareProtocolSettings &);
|
||||
FileShareProtocolSettings settings() const;
|
||||
|
||||
private:
|
||||
Internal::Ui::FileShareProtocolSettingsWidget m_ui;
|
||||
explicit FileShareProtocolSettingsPage(FileShareProtocolSettings *s);
|
||||
};
|
||||
|
||||
class FileShareProtocolSettingsPage : public Core::IOptionsPage
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit FileShareProtocolSettingsPage(const QSharedPointer<FileShareProtocolSettings> &s);
|
||||
|
||||
QWidget *widget() override;
|
||||
void apply() override;
|
||||
void finish() override { }
|
||||
|
||||
private:
|
||||
const QSharedPointer<FileShareProtocolSettings> m_settings;
|
||||
QPointer<FileShareProtocolSettingsWidget> m_widget;
|
||||
};
|
||||
} // namespace CodePaster
|
||||
|
@@ -1,101 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>CodePaster::Internal::FileShareProtocolSettingsWidget</class>
|
||||
<widget class="QWidget" name="CodePaster::Internal::FileShareProtocolSettingsWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>438</width>
|
||||
<height>101</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string/>
|
||||
</property>
|
||||
<layout class="QFormLayout" name="formLayout">
|
||||
<property name="fieldGrowthPolicy">
|
||||
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
|
||||
</property>
|
||||
<item row="0" column="0" colspan="2">
|
||||
<widget class="QLabel" name="helpLabel">
|
||||
<property name="text">
|
||||
<string>The fileshare-based paster protocol allows for sharing code snippets using simple files on a shared network drive. Files are never deleted.</string>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeType">
|
||||
<enum>QSizePolicy::Fixed</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>3</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="pathLabel">
|
||||
<property name="text">
|
||||
<string>&Path:</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>pathChooser</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="Utils::PathChooser" name="pathChooser" native="true"/>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="displayCountLabel">
|
||||
<property name="text">
|
||||
<string>&Display:</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>displayCountSpinBox</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QSpinBox" name="displayCountSpinBox">
|
||||
<property name="suffix">
|
||||
<string>entries</string>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>100</number>
|
||||
</property>
|
||||
<property name="value">
|
||||
<number>11</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>Utils::PathChooser</class>
|
||||
<extends>QWidget</extends>
|
||||
<header location="global">utils/pathchooser.h</header>
|
||||
<container>1</container>
|
||||
<slots>
|
||||
<signal>editingFinished()</signal>
|
||||
<signal>browsingFinished()</signal>
|
||||
</slots>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
Reference in New Issue
Block a user