Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize bookmarks at save time #1307

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/qtgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ add_source_files(SRCS_LIST
ctk/ctkRangeSlider.h
demod_options.cpp
demod_options.h
dlg_saveablestring.cpp
dlg_saveablestring.h
dockaudio.cpp
dockaudio.h
dockbookmarks.cpp
Expand Down
37 changes: 16 additions & 21 deletions src/qtgui/bookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,19 @@ bool Bookmarks::save()
stream << QString("# Tag name").leftJustified(20) + "; " +
QString(" color") << '\n';

QMap<QString, TagInfo::sptr> usedTags;
for (int iBookmark = 0; iBookmark < m_BookmarkList.size(); iBookmark++)
std::map<QString, TagInfo::sptr> usedTags;
for (const auto &bookmarkInfo: m_BookmarkList)
{
BookmarkInfo& info = m_BookmarkList[iBookmark];
for (QList<TagInfo::sptr>::iterator iTag = info.tags.begin(); iTag < info.tags.end(); ++iTag)
for (const auto &tag: bookmarkInfo.tags)
{
usedTags.insert((*iTag)->name, *iTag);
usedTags.emplace(tag->name, tag);
}
}

for (QMap<QString, TagInfo::sptr>::iterator i = usedTags.begin(); i != usedTags.end(); i++)
for (const auto &tag: usedTags)
{
TagInfo::sptr info = *i;
stream << info->name.leftJustified(20) + "; " + info->color.name() << '\n';
auto tagInfo = tag.second;
stream << tagInfo->name.leftJustified(20) + "; " + tagInfo->color.name() << '\n';
}

stream << '\n';
Expand All @@ -180,22 +179,18 @@ bool Bookmarks::save()
QString("Bandwidth").rightJustified(10) + "; " +
QString("Tags") << '\n';

for (int i = 0; i < m_BookmarkList.size(); i++)
for (const auto &bookmarkInfo: m_BookmarkList)
{
BookmarkInfo& info = m_BookmarkList[i];
QString line = QString::number(info.frequency).rightJustified(12) +
"; " + info.name.leftJustified(25) + "; " +
info.modulation.leftJustified(20)+ "; " +
QString::number(info.bandwidth).rightJustified(10) + "; ";
for(int iTag = 0; iTag<info.tags.size(); ++iTag)
QString line = QString::number(bookmarkInfo.frequency).rightJustified(12) +
"; " + bookmarkInfo.name.leftJustified(25) + "; " +
bookmarkInfo.modulation.leftJustified(20)+ "; " +
QString::number(bookmarkInfo.bandwidth).rightJustified(10) + "; ";
QStringList tags;
for(const auto &tagInfo: bookmarkInfo.tags)
{
TagInfo::sptr tag = info.tags[iTag];
if (iTag!=0)
{
line.append(",");
}
line.append(tag->name);
tags.append(tagInfo->name);
}
line.append(tags.join(','));

stream << line << '\n';
}
Expand Down
4 changes: 4 additions & 0 deletions src/qtgui/bookmarkstaglist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
#include "bookmarkstaglist.h"
#include "bookmarks.h"
#include "dlg_saveablestring.h"
#include <QColorDialog>
#include <stdio.h>
#include <QMenu>
Expand All @@ -48,6 +49,9 @@ BookmarksTagList::BookmarksTagList(QWidget *parent, bool bShowUntagged )
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
setSortingEnabled(true);

LineEditDelegateSaveableString* DelegateTagName = new LineEditDelegateSaveableString(this);
this->setItemDelegateForColumn(1, DelegateTagName);
}

void BookmarksTagList::on_cellClicked(int row, int column)
Expand Down
64 changes: 64 additions & 0 deletions src/qtgui/dlg_saveablestring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* -*- c++ -*- */
/*
* Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
* https://gqrx.dk/
*
* Copyright 2023 0penBrain
*
* Gqrx is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* Gqrx is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gqrx; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/

#include "dlg_saveablestring.h"

#include <QDebug>
#include <QLineEdit>

LineEditDelegateSaveableString::LineEditDelegateSaveableString(QObject *parent)
:QStyledItemDelegate(parent)
{

}

#include <QDebug>
QWidget *LineEditDelegateSaveableString::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
auto* editor = QStyledItemDelegate::createEditor(parent, option, index);
auto* lineedit = qobject_cast<QLineEdit*>(editor);
if (!lineedit)
{
qWarning() << __FUNCTION__ << " : " << "Editor cannot be cast to LineEdit, entry will not be validated";
}
else
{
lineedit->setValidator(new SaveableStringValidator(lineedit));
}
return editor;
}

SaveableStringValidator::SaveableStringValidator(QObject *parent)
:QValidator(parent)
{

}

QValidator::State SaveableStringValidator::validate(QString &input, int &pos) const
{
if (input.contains(';') || input.contains(','))
{
return QValidator::Invalid;
}
return QValidator::Acceptable;
}
47 changes: 47 additions & 0 deletions src/qtgui/dlg_saveablestring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* -*- c++ -*- */
/*
* Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
* https://gqrx.dk/
*
* Copyright 2023 0penBrain
*
* Gqrx is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* Gqrx is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gqrx; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef DLG_SAVEABLESTRING_H
#define DLG_SAVEABLESTRING_H

#include <QStyledItemDelegate>
#include <QValidator>

class LineEditDelegateSaveableString : public QStyledItemDelegate
{
Q_OBJECT
public:
LineEditDelegateSaveableString(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
//void setEditorData(QWidget *editor, const QModelIndex &index) const;
//void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
};

class SaveableStringValidator : public QValidator
{
Q_OBJECT
public:
SaveableStringValidator(QObject *parent = 0);
QValidator::State validate(QString &input, int &pos) const;
};

#endif // DLG_SAVEABLESTRING_H
4 changes: 4 additions & 0 deletions src/qtgui/dockbookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "bookmarks.h"
#include "bookmarkstaglist.h"
#include "dlg_saveablestring.h"
#include "dockbookmarks.h"
#include "dockrxopt.h"
#include "qtcolorpicker.h"
Expand All @@ -56,6 +57,9 @@ DockBookmarks::DockBookmarks(QWidget *parent) :
ComboBoxDelegateModulation* delegateModulation = new ComboBoxDelegateModulation(this);
ui->tableViewFrequencyList->setItemDelegateForColumn(2, delegateModulation);

LineEditDelegateSaveableString* delegateBookmarkName = new LineEditDelegateSaveableString(this);
ui->tableViewFrequencyList->setItemDelegateForColumn(1, delegateBookmarkName);

// Bookmarks Context menu
contextmenu = new QMenu(this);
// MenuItem Delete
Expand Down