From bf8750e20ac12e35146d07daa638957d5e87dd13 Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage Vallee <elv1313@gmail.com>
Date: Mon, 21 Dec 2015 09:04:48 -0500
Subject: [PATCH] ringtone: Implement missing bits

When porting all objects to the Collection system, the RingtoneModel
gained the ability to keep a global list of RingTones, add folders
and so on. However, this was never finished due to time constraints.

This commit complete the basic features. It is now good enough to be
used by the clients.

Tuleap: #204
Change-Id: I36a14b811b36646aa62f87aeccdbab130f416f43
---
 src/localringtonecollection.cpp |  2 ++
 src/ringtonemodel.cpp           | 44 +++++++++++++++++++++++++++------
 src/ringtonemodel.h             |  1 +
 3 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/src/localringtonecollection.cpp b/src/localringtonecollection.cpp
index fa33516a..ef4f8268 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 fe825fa4..5778af40 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 683623a8..96861f0f 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();
 
-- 
GitLab