bookmarks.cpp 7.6 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1
/* -*- c++ -*- */
M
manjaro 已提交
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
/*
 * Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
 *           http://gqrx.dk/
 *
 * Copyright 2013 Christian Lindner DL2VCL, Stefano Leucci.
 *
 * 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 <Qt>
#include <QFile>
#include <QStringList>
#include <QTextStream>
#include <QString>
#include <QSet>
#include <algorithm>
#include "bookmarks.h"
#include <stdio.h>
#include <wchar.h>
丁劲犇's avatar
丁劲犇 已提交
33
#include <algorithm>
M
manjaro 已提交
34 35 36 37 38 39
const QColor TagInfo::DefaultColor(Qt::lightGray);
const QString TagInfo::strUntagged("Untagged");
Bookmarks* Bookmarks::m_pThis = 0;

Bookmarks::Bookmarks()
{
丁劲犇's avatar
丁劲犇 已提交
40 41
	 TagInfo tag(TagInfo::strUntagged);
	 m_TagList.append(tag);
M
manjaro 已提交
42 43 44 45
}

void Bookmarks::create()
{
丁劲犇's avatar
丁劲犇 已提交
46
	m_pThis = new Bookmarks;
M
manjaro 已提交
47 48 49 50
}

Bookmarks& Bookmarks::Get()
{
丁劲犇's avatar
丁劲犇 已提交
51
	return *m_pThis;
M
manjaro 已提交
52 53 54 55
}

void Bookmarks::setConfigDir(const QString& cfg_dir)
{
丁劲犇's avatar
丁劲犇 已提交
56 57
	m_bookmarksFile = cfg_dir + "/bookmarks.csv";
	printf("BookmarksFile is %s\n", m_bookmarksFile.toStdString().c_str());
M
manjaro 已提交
58 59 60 61
}

void Bookmarks::add(BookmarkInfo &info)
{
丁劲犇's avatar
丁劲犇 已提交
62 63 64 65
	m_BookmarkList.append(info);
	std::stable_sort(m_BookmarkList.begin(),m_BookmarkList.end());
	save();
	emit( BookmarksChanged() );
M
manjaro 已提交
66 67 68 69
}

void Bookmarks::remove(int index)
{
丁劲犇's avatar
丁劲犇 已提交
70 71 72
	m_BookmarkList.removeAt(index);
	save();
	emit BookmarksChanged();
M
manjaro 已提交
73 74 75 76
}

bool Bookmarks::load()
{
丁劲犇's avatar
丁劲犇 已提交
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
	QFile file(m_bookmarksFile);
	if (file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		m_BookmarkList.clear();
		m_TagList.clear();

		// always create the "Untagged" entry.
		findOrAddTag(TagInfo::strUntagged);

		// Read Tags, until first empty line.
		while (!file.atEnd())
		{
			QString line = QString::fromUtf8(file.readLine().trimmed());

			if(line.isEmpty())
				break;

			if(line.startsWith("#"))
				continue;

			QStringList strings = line.split(";");
			if(strings.count() == 2)
			{
				TagInfo &info = findOrAddTag(strings[0]);
				info.color = QColor(strings[1].trimmed());
			}
			else
			{
				printf("\nBookmarks: Ignoring Line:\n  %s\n", line.toLatin1().data());
			}
		}
		std::sort(m_TagList.begin(),m_TagList.end());

		// Read Bookmarks, after first empty line.
		while (!file.atEnd())
		{
			QString line = QString::fromUtf8(file.readLine().trimmed());
			if(line.isEmpty() || line.startsWith("#"))
				continue;

			QStringList strings = line.split(";");
			if(strings.count() == 5)
			{
				BookmarkInfo info;
				info.frequency  = strings[0].toLongLong();
				info.name       = strings[1].trimmed();
				info.modulation = strings[2].trimmed();
				info.bandwidth  = strings[3].toInt();
				// Multiple Tags may be separated by comma.
				QString strTags = strings[4];
				QStringList TagList = strTags.split(",");
				for(int iTag=0; iTag<TagList.size(); ++iTag)
				{
				  info.tags.append(&findOrAddTag(TagList[iTag].trimmed()));
				}

				m_BookmarkList.append(info);
			}
			else
			{
				printf("\nBookmarks: Ignoring Line:\n  %s\n", line.toLatin1().data());
			}
		}
		file.close();
		std::stable_sort(m_BookmarkList.begin(),m_BookmarkList.end());

		emit BookmarksChanged();
		return true;
	}
	return false;
M
manjaro 已提交
147 148 149 150 151
}

//FIXME: Commas in names
bool Bookmarks::save()
{
丁劲犇's avatar
丁劲犇 已提交
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
	QFile file(m_bookmarksFile);
	if(file.open(QFile::WriteOnly | QFile::Truncate | QIODevice::Text))
	{
		QTextStream stream(&file);

		stream << QString("# Tag name").leftJustified(20) + "; " +
				  QString(" color") << Qt::endl;

		QSet<TagInfo*> usedTags;
		for (int iBookmark = 0; iBookmark < m_BookmarkList.size(); iBookmark++)
		{
			BookmarkInfo& info = m_BookmarkList[iBookmark];
			for(int iTag = 0; iTag < info.tags.size(); ++iTag)
			{
			  TagInfo& tag = *info.tags[iTag];
			  usedTags.insert(&tag);
			}
		}

		for (QSet<TagInfo*>::iterator i = usedTags.begin(); i != usedTags.end(); i++)
		{
			TagInfo& info = **i;
			stream << info.name.leftJustified(20) + "; " + info.color.name() << Qt::endl;
		}

		stream << Qt::endl;

		stream << QString("# Frequency").leftJustified(12) + "; " +
				  QString("Name").leftJustified(25)+ "; " +
				  QString("Modulation").leftJustified(20) + "; " +
				  QString("Bandwidth").rightJustified(10) + "; " +
				  QString("Tags") << Qt::endl;

		for (int i = 0; i < m_BookmarkList.size(); i++)
		{
			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)
			{
				TagInfo& tag = *info.tags[iTag];
				if(iTag!=0)
				{
					line.append(",");
				}
				line.append(tag.name);
			}

			stream << line << Qt::endl;
		}

		file.close();
		return true;
	}
	return false;
M
manjaro 已提交
209 210 211 212
}

QList<BookmarkInfo> Bookmarks::getBookmarksInRange(qint64 low, qint64 high)
{
丁劲犇's avatar
丁劲犇 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	BookmarkInfo info;
	info.frequency=low;
	QList<BookmarkInfo>::const_iterator lb = std::lower_bound(m_BookmarkList.begin(), m_BookmarkList.end(),info);
	info.frequency=high;
	QList<BookmarkInfo>::const_iterator ub = std::upper_bound(m_BookmarkList.begin(), m_BookmarkList.end(),info);

	QList<BookmarkInfo> found;

	while (lb != ub)
	{
		const BookmarkInfo& info = *lb;
		//if(info.IsActive())
		{
		  found.append(info);
		}
		lb++;
	}

	return found;
M
manjaro 已提交
232 233 234 235 236

}

TagInfo &Bookmarks::findOrAddTag(QString tagName)
{
丁劲犇's avatar
丁劲犇 已提交
237
	tagName = tagName.trimmed();
M
manjaro 已提交
238

丁劲犇's avatar
丁劲犇 已提交
239 240
	if (tagName.isEmpty())
		tagName=TagInfo::strUntagged;
M
manjaro 已提交
241

丁劲犇's avatar
丁劲犇 已提交
242
	int idx = getTagIndex(tagName);
M
manjaro 已提交
243

丁劲犇's avatar
丁劲犇 已提交
244 245
	if (idx != -1)
		return m_TagList[idx];
M
manjaro 已提交
246

丁劲犇's avatar
丁劲犇 已提交
247 248 249 250 251
	TagInfo info;
	info.name=tagName;
	m_TagList.append(info);
	emit TagListChanged();
	return m_TagList.last();
M
manjaro 已提交
252 253 254 255
}

bool Bookmarks::removeTag(QString tagName)
{
丁劲犇's avatar
丁劲犇 已提交
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
	tagName = tagName.trimmed();

	// Do not delete "Untagged" tag.
	if(tagName.compare(TagInfo::strUntagged, tagName)==0)
		return false;

	int idx = getTagIndex(tagName);
	if (idx == -1)
		return false;

	// Delete Tag from all Bookmarks that use it.
	TagInfo* pTagToDelete = &m_TagList[idx];
	for(int i=0; i<m_BookmarkList.size(); ++i)
	{
		BookmarkInfo& bmi = m_BookmarkList[i];
		for(int t=0; t<bmi.tags.size(); ++t)
		{
			TagInfo* pTag = bmi.tags[t];
			if(pTag == pTagToDelete)
			{
				if(bmi.tags.size()>1) bmi.tags.removeAt(t);
				else bmi.tags[0] = &findOrAddTag(TagInfo::strUntagged);
			}
		}
	}

	// Delete Tag.
	m_TagList.removeAt(idx);

	emit BookmarksChanged();
	emit TagListChanged();

	return true;
M
manjaro 已提交
289 290 291 292
}

bool Bookmarks::setTagChecked(QString tagName, bool bChecked)
{
丁劲犇's avatar
丁劲犇 已提交
293 294 295 296 297 298
	int idx = getTagIndex(tagName);
	if (idx == -1) return false;
	m_TagList[idx].active = bChecked;
	emit BookmarksChanged();
	emit TagListChanged();
	return true;
M
manjaro 已提交
299 300 301 302
}

int Bookmarks::getTagIndex(QString tagName)
{
丁劲犇's avatar
丁劲犇 已提交
303 304 305 306 307 308 309 310
	tagName = tagName.trimmed();
	for (int i = 0; i < m_TagList.size(); i++)
	{
		if (m_TagList[i].name == tagName)
			return i;
	}

	return -1;
M
manjaro 已提交
311 312 313 314
}

const QColor BookmarkInfo::GetColor() const
{
丁劲犇's avatar
丁劲犇 已提交
315 316 317 318 319 320 321 322 323
	for(int iTag=0; iTag<tags.size(); ++iTag)
	{
		TagInfo& tag = *tags[iTag];
		if(tag.active)
		{
			return tag.color;
		}
	}
	return TagInfo::DefaultColor;
M
manjaro 已提交
324 325 326 327
}

bool BookmarkInfo::IsActive() const
{
丁劲犇's avatar
丁劲犇 已提交
328 329 330 331 332 333 334 335 336 337 338
	bool bActive = false;
	for(int iTag=0; iTag<tags.size(); ++iTag)
	{
		TagInfo& tag = *tags[iTag];
		if(tag.active)
		{
			bActive = true;
			break;
		}
	}
	return bActive;
M
manjaro 已提交
339
}