diff --git a/src/localringtonecollection.cpp b/src/localringtonecollection.cpp index fa33516a6b8f29639042bb8d2ce9f414a70b9e56..ef4f8268d2b87926121acc64c48f4d57db19c201 100644 --- a/src/localringtonecollection.cpp +++ b/src/localringtonecollection.cpp @@ -265,11 +265,13 @@ void Serializable::RingtoneNode::read(const QJsonObject &json) { ringtone = new Ringtone(); ringtone->setPath(json["path"].toString()); + ringtone->setName(json["name"].toString()); } void Serializable::RingtoneNode::write(QJsonObject& json) { json["path"] = ringtone->path().path(); + json["name"] = ringtone->name(); } #include <localringtonecollection.moc> diff --git a/src/ringtonemodel.cpp b/src/ringtonemodel.cpp index fe825fa4273faab72e13c5029169e83c2a5cbad2..5778af40fc5a0e94dcb1d78a84d090d23f8516a6 100644 --- a/src/ringtonemodel.cpp +++ b/src/ringtonemodel.cpp @@ -39,11 +39,13 @@ public: RingtoneModelPrivate(RingtoneModel*); //Attributes - QVector<Ringtone*> m_lRingtone ; - QTimer* m_pTimer ; - Ringtone* m_pCurrent ; - QHash<Account*,int> m_hCurrent ; - QHash<Account*,QItemSelectionModel*> m_hSelectionModels; + QVector<Ringtone*> m_lRingtone ; + QTimer* m_pTimer ; + Ringtone* m_pCurrent ; + QHash<Account*,int> m_hCurrent ; + QHash<Account*,QItemSelectionModel*> m_hSelectionModels ; + LocalRingtoneCollection* m_pCollection ; + QHash<const Ringtone*,Account*> m_hPendingSelection; //Helpers int currentIndex(Account* a) const; @@ -71,7 +73,7 @@ RingtoneModel::RingtoneModel(QObject* parent) { // ConfigurationManagerInterface& configurationManager = ConfigurationManager::instance(); - addCollection<LocalRingtoneCollection>(); + d_ptr->m_pCollection = addCollection<LocalRingtoneCollection>(); } RingtoneModel& RingtoneModel::instance() @@ -139,7 +141,7 @@ int RingtoneModel::columnCount( const QModelIndex& parent ) const { if (parent.isValid()) return 0; - return 2; //Name, then an empty one for widgets + return 1; } Qt::ItemFlags RingtoneModel::flags( const QModelIndex& index ) const @@ -245,6 +247,17 @@ bool RingtoneModel::addItemCallback(const Ringtone* item) beginInsertRows(QModelIndex(),d_ptr->m_lRingtone.size(),d_ptr->m_lRingtone.size()); d_ptr->m_lRingtone << const_cast<Ringtone*>(item); endInsertRows(); + + if (Account* a = d_ptr->m_hPendingSelection[item]) { + + if (auto sm = d_ptr->m_hSelectionModels[a]) + sm->setCurrentIndex(index(rowCount()-1,0), QItemSelectionModel::ClearAndSelect); + else + a->setRingtonePath(item->path().path()); + + d_ptr->m_hPendingSelection[item] = nullptr; + } + return true; } @@ -254,4 +267,21 @@ bool RingtoneModel::removeItemCallback(const Ringtone* item) return true; } +bool RingtoneModel::add(const QUrl& path, Account* autoSelect) +{ + auto r = new Ringtone(this); + r->setPath(path); + r->setName(QFile(path.path()).fileName()); + + if (autoSelect) + d_ptr->m_hPendingSelection[r] = autoSelect; + + d_ptr->m_pCollection->add(r); + + //TODO check the file type + //TODO avoid duplicates + + return true; +} + #include <ringtonemodel.moc> diff --git a/src/ringtonemodel.h b/src/ringtonemodel.h index 683623a890d15e6d3b45d30b45982c129ff2cc9e..96861f0fb6d91ae2e2a6277c9ee650602a8e8bf9 100644 --- a/src/ringtonemodel.h +++ b/src/ringtonemodel.h @@ -57,6 +57,7 @@ public: //Mutator void play(const QModelIndex& index); + bool add(const QUrl& path, Account* autoSelect = nullptr); static RingtoneModel& instance();