Re-add bookmarking of TuneIn categories
BUG:252
This commit is contained in:
committed by
craig.p.drummond
parent
559ed9a64d
commit
8a36efbcbc
@@ -130,6 +130,112 @@ static QString categoryCacheName(const QString &name, bool createDir=false)
|
||||
return Utils::cacheDir(StreamsModel::constCacheDir, createDir)+name+StreamsModel::constCacheExt;
|
||||
}
|
||||
|
||||
static QString categoryBookmarksName(const QString &name, bool createDir=false)
|
||||
{
|
||||
return Utils::configDir(QLatin1String("bookmarks"), createDir)+name+StreamsModel::constCacheExt;
|
||||
}
|
||||
|
||||
void StreamsModel::CategoryItem::removeBookmarks()
|
||||
{
|
||||
if (bookmarksName.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
QString fileName=categoryBookmarksName(bookmarksName);
|
||||
if (QFile::exists(fileName)) {
|
||||
QFile::remove(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
void StreamsModel::CategoryItem::saveBookmarks()
|
||||
{
|
||||
if (bookmarksName.isEmpty() || !supportsBookmarks) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (Item *child, children) {
|
||||
if (child->isCategory()) {
|
||||
CategoryItem *cat=static_cast<CategoryItem *>(child);
|
||||
if (cat->isBookmarks) {
|
||||
if (cat->children.count()) {
|
||||
QFile file(categoryBookmarksName(bookmarksName, true));
|
||||
QtIOCompressor compressor(&file);
|
||||
compressor.setStreamFormat(QtIOCompressor::GzipFormat);
|
||||
if (compressor.open(QIODevice::WriteOnly)) {
|
||||
QXmlStreamWriter doc(&compressor);
|
||||
doc.writeStartDocument();
|
||||
doc.writeStartElement("bookmarks");
|
||||
doc.writeAttribute("version", "1.0");
|
||||
doc.setAutoFormatting(false);
|
||||
foreach (Item *i, cat->children) {
|
||||
doc.writeStartElement("bookmark");
|
||||
doc.writeAttribute("name", i->name);
|
||||
doc.writeAttribute("url", i->url);
|
||||
doc.writeEndElement();
|
||||
}
|
||||
doc.writeEndElement();
|
||||
doc.writeEndElement();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QList<StreamsModel::Item *> StreamsModel::CategoryItem::loadBookmarks()
|
||||
{
|
||||
QList<Item *> newItems;
|
||||
if (bookmarksName.isEmpty() || !supportsBookmarks) {
|
||||
return newItems;
|
||||
}
|
||||
|
||||
QString fileName=categoryBookmarksName(bookmarksName);
|
||||
if (!QFile::exists(fileName)) {
|
||||
return newItems;
|
||||
}
|
||||
|
||||
QFile file(fileName);
|
||||
QtIOCompressor compressor(&file);
|
||||
compressor.setStreamFormat(QtIOCompressor::GzipFormat);
|
||||
if (compressor.open(QIODevice::ReadOnly)) {
|
||||
QXmlStreamReader doc(&compressor);
|
||||
while (!doc.atEnd()) {
|
||||
doc.readNext();
|
||||
|
||||
if (doc.isStartElement() && QLatin1String("bookmark")==doc.name()) {
|
||||
QString name=doc.attributes().value("name").toString();
|
||||
QString url=doc.attributes().value("url").toString();
|
||||
if (!name.isEmpty() && !url.isEmpty()) {
|
||||
newItems.append(new CategoryItem(url, name, this));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return newItems;
|
||||
}
|
||||
|
||||
StreamsModel::CategoryItem * StreamsModel::CategoryItem::getBookmarksCategory()
|
||||
{
|
||||
foreach (Item *i, children) {
|
||||
if (i->isCategory() && static_cast<CategoryItem *>(i)->isBookmarks) {
|
||||
return static_cast<CategoryItem *>(i);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
StreamsModel::CategoryItem * StreamsModel::CategoryItem::createBookmarksCategory()
|
||||
{
|
||||
Icon icon=Icon("bookmarks");
|
||||
if (icon.isNull()) {
|
||||
icon=Icon("user-bookmarks");
|
||||
}
|
||||
CategoryItem *bookmarkCat = new CategoryItem(QString(), i18n("Bookmarks"), this, icon);
|
||||
bookmarkCat->state=CategoryItem::Fetched;
|
||||
bookmarkCat->isBookmarks=true;
|
||||
return bookmarkCat;
|
||||
}
|
||||
|
||||
void StreamsModel::CategoryItem::removeCache()
|
||||
{
|
||||
if (childrenHaveCache) {
|
||||
@@ -195,6 +301,9 @@ static void saveStream(QXmlStreamWriter &doc, const StreamsModel::Item *item)
|
||||
|
||||
static void saveCategory(QXmlStreamWriter &doc, const StreamsModel::CategoryItem *cat)
|
||||
{
|
||||
if (cat->isBookmarks) {
|
||||
return;
|
||||
}
|
||||
doc.writeStartElement("category");
|
||||
doc.writeAttribute("name", cat->name);
|
||||
if (cat->isAll) {
|
||||
@@ -336,7 +445,8 @@ StreamsModel::StreamsModel(QObject *parent)
|
||||
, favouritesModified(false)
|
||||
, favouritesSaveTimer(0)
|
||||
{
|
||||
tuneIn=new CategoryItem(constRadioTimeUrl+QLatin1String("?locale=")+QLocale::system().name(), i18n("TuneIn"), root, getIcon("tunein"));
|
||||
tuneIn=new CategoryItem(constRadioTimeUrl+QLatin1String("?locale=")+QLocale::system().name(), i18n("TuneIn"), root, getIcon("tunein"), QString(), "tunein");
|
||||
tuneIn->supportsBookmarks=true;
|
||||
root->children.append(tuneIn);
|
||||
root->children.append(new CategoryItem(constIceCastUrl, i18n("IceCast"), root, getIcon("icecast"), "icecast"));
|
||||
root->children.append(new CategoryItem(constShoutCastUrl, i18n("ShoutCast"), root, getIcon("shoutcast")));
|
||||
@@ -436,7 +546,13 @@ QVariant StreamsModel::data(const QModelIndex &index, int role) const
|
||||
}
|
||||
break;
|
||||
case ItemView::Role_Actions:
|
||||
if (!item->isCategory()){
|
||||
if (item->isCategory()){
|
||||
if (static_cast<const CategoryItem *>(item)->canBookmark) {
|
||||
QVariant v;
|
||||
v.setValue<QList<Action *> >(QList<Action *>() << StdActions::self()->addBookmarkAction);
|
||||
return v;
|
||||
}
|
||||
} else {
|
||||
QVariant v;
|
||||
v.setValue<QList<Action *> >(QList<Action *>() << StdActions::self()->replacePlayQueueAction);
|
||||
return v;
|
||||
@@ -655,6 +771,76 @@ void StreamsModel::updateFavouriteStream(Item *item)
|
||||
emit dataChanged(index, index);
|
||||
}
|
||||
|
||||
void StreamsModel::addBookmark(const QString &url, const QString &name, CategoryItem *bookmarkParentCat)
|
||||
{
|
||||
if (!bookmarkParentCat) {
|
||||
bookmarkParentCat=tuneIn;
|
||||
}
|
||||
|
||||
if (bookmarkParentCat && !url.isEmpty() && !name.isEmpty()) {
|
||||
CategoryItem *bookmarkCat=bookmarkParentCat->getBookmarksCategory();
|
||||
if (!bookmarkCat) {
|
||||
QModelIndex index=createIndex(bookmarkParentCat->parent->children.indexOf(bookmarkParentCat), 0, (void *)bookmarkParentCat);
|
||||
beginInsertRows(index, bookmarkParentCat->children.count(), bookmarkParentCat->children.count());
|
||||
bookmarkCat=bookmarkParentCat->createBookmarksCategory();
|
||||
bookmarkParentCat->children.append(bookmarkCat);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
foreach (Item *i, bookmarkCat->children) {
|
||||
if (i->url==url) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
QModelIndex index=createIndex(bookmarkCat->parent->children.indexOf(bookmarkCat), 0, (void *)bookmarkCat);
|
||||
beginInsertRows(index, bookmarkCat->children.count(), bookmarkCat->children.count());
|
||||
bookmarkCat->children.append(new CategoryItem(url, name, bookmarkCat));
|
||||
endInsertRows();
|
||||
bookmarkParentCat->saveBookmarks();
|
||||
}
|
||||
}
|
||||
|
||||
void StreamsModel::removeBookmark(const QModelIndex &index)
|
||||
{
|
||||
Item *item=toItem(index);
|
||||
|
||||
if (item->isCategory() && item->parent && item->parent->isBookmarks) {
|
||||
CategoryItem *bookmarkCat=item->parent; // 'Bookmarks'
|
||||
CategoryItem *cat=bookmarkCat->parent; // e.g. 'TuneIn'
|
||||
if (1==bookmarkCat->children.count()) { // Only 1 bookark, so remove 'Bookmarks' folder...
|
||||
int pos=cat->children.indexOf(bookmarkCat);
|
||||
QModelIndex index=createIndex(cat->parent->children.indexOf(cat), 0, (void *)cat);
|
||||
beginRemoveRows(index, pos, pos);
|
||||
delete cat->children.takeAt(pos);
|
||||
endRemoveRows();
|
||||
cat->removeBookmarks();
|
||||
} else if (!bookmarkCat->children.isEmpty()) { // More than 1 bookmark...
|
||||
int pos=bookmarkCat->children.indexOf(item);
|
||||
QModelIndex index=createIndex(bookmarkCat->parent->children.indexOf(bookmarkCat), 0, (void *)bookmarkCat);
|
||||
beginRemoveRows(index, pos, pos);
|
||||
delete bookmarkCat->children.takeAt(pos);
|
||||
endRemoveRows();
|
||||
cat->saveBookmarks();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StreamsModel::removeAllBookmarks(const QModelIndex &index)
|
||||
{
|
||||
Item *item=toItem(index);
|
||||
|
||||
if (item->isCategory() && static_cast<CategoryItem *>(item)->isBookmarks) {
|
||||
CategoryItem *cat=item->parent; // e.g. 'TuneIn'
|
||||
int pos=cat->children.indexOf(item);
|
||||
QModelIndex index=createIndex(cat->parent->children.indexOf(cat), 0, (void *)cat);
|
||||
beginRemoveRows(index, pos, pos);
|
||||
delete cat->children.takeAt(pos);
|
||||
endRemoveRows();
|
||||
cat->removeBookmarks();
|
||||
}
|
||||
}
|
||||
|
||||
QModelIndex StreamsModel::favouritesIndex() const
|
||||
{
|
||||
return createIndex(root->children.indexOf(favourites), 0, (void *)favourites);
|
||||
@@ -771,6 +957,43 @@ void StreamsModel::jobFinished()
|
||||
}
|
||||
}
|
||||
|
||||
if (cat && cat->parent==root && cat->supportsBookmarks) {
|
||||
QList<Item *> bookmarks=cat->loadBookmarks();
|
||||
if (bookmarks.count()) {
|
||||
CategoryItem *bookmarksCat=cat->getBookmarksCategory();
|
||||
|
||||
if (bookmarksCat) {
|
||||
QList<Item *> newBookmarks;
|
||||
foreach (Item *bm, bookmarks) {
|
||||
foreach (Item *ex, bookmarksCat->children) {
|
||||
if (ex->url==bm->url) {
|
||||
delete bm;
|
||||
bm=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (bm) {
|
||||
newBookmarks.append(bm);
|
||||
bm->parent=bookmarksCat;
|
||||
}
|
||||
}
|
||||
if (newBookmarks.count()) {
|
||||
QModelIndex index=createIndex(bookmarksCat->parent->children.indexOf(bookmarksCat), 0, (void *)bookmarksCat);
|
||||
beginInsertRows(index, bookmarksCat->children.count(), (bookmarksCat->children.count()+newBookmarks.count())-1);
|
||||
bookmarksCat->children+=newBookmarks;
|
||||
endInsertRows();
|
||||
}
|
||||
} else {
|
||||
bookmarksCat=cat->createBookmarksCategory();
|
||||
foreach (Item *i, bookmarks) {
|
||||
i->parent=bookmarksCat;
|
||||
}
|
||||
bookmarksCat->children=bookmarks;
|
||||
newItems.append(bookmarksCat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!newItems.isEmpty()) {
|
||||
beginInsertRows(index, cat->children.count(), (cat->children.count()+newItems.count())-1);
|
||||
cat->children+=newItems;
|
||||
@@ -1297,6 +1520,7 @@ StreamsModel::Item * StreamsModel::parseRadioTimeEntry(QXmlStreamReader &doc, Ca
|
||||
item=new Item(url, text, parent, subText);
|
||||
} else {
|
||||
cat=new CategoryItem(url, text, parent);
|
||||
cat->canBookmark=!url.isEmpty();
|
||||
item=cat;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user