Example #1
0
/*!
 * Clears the model.
 */
void ServerModel::resetModel()
{
    clear();

    QStandardItem *root = invisibleRootItem();

    QStandardItem *i = new QStandardItem();
    QStringList l;
    l << tr("Name") << tr("Host");
    setColumnCount(2);
    setHorizontalHeaderItem(0, i);
    setHorizontalHeaderLabels(l);


   QStringList netlist = smgr.networkList();

   if (netlist.contains("NONE")) { // "None" network is a section with servers not assigned to a network.
       QHash<QString,QString> sl = smgr.serverList("NONE");
       QHashIterator<QString,QString> i(sl);
       while (i.hasNext()) {
           i.next();
           // Key: Server name
           // Value: host:port|pass
           QString name = i.key();
           QString detail = i.value();

           QString host; // hostname with port, e.g. irc.network.org:6667
           host = detail.split('|')[0];

           QStandardItem *itemname = new QStandardItem(QIcon(":/options/gfx/server.png"), name);
           QStandardItem *itemhost = new QStandardItem(host);
           QList<QStandardItem*> list;
           list << itemname << itemhost;

           root->appendRow(list);
           hostmap.insert(host, indexFromItem(itemname));
           nonemap.insert(name, indexFromItem(itemname));

       }
   }

   for (int i = 0; i <= netlist.count()-1; ++i) {

       if (netlist[i] == "NONE")
           continue; // The "None" network already taken care of - ignore.

       QString data = smgr.defaultServer(netlist[i]);
       QString host = data.split('|')[0];

       QStandardItem *pname = new QStandardItem(QIcon(":/options/gfx/network.png"), netlist[i]); // parent name
       QStandardItem *phost = new QStandardItem(host); // parent host
       QList<QStandardItem*> list;
       list << pname << phost;

       root->appendRow(list);
       hostmap.insert(host, pname->index());
       netmap.insert(netlist[i], pname->index());

       QHash<QString,QString> sl = smgr.serverList(netlist[i]);
       QHashIterator<QString,QString> sli(sl);
       while (sli.hasNext()) {
           sli.next();
           // Key: Server name
           // Value: host:port|pass
           QString name = sli.key();
           if (name == "DEFAULT")
               continue; // The default value already taken care of, it's the address of parent item.
           QString detail = sli.value();
           QString host; // hostname with port, e.g. irc.network.org:6667
           host = detail.split('|')[0];

           QStandardItem *itemname = new QStandardItem(QIcon(":/options/gfx/server.png"), name); // parent name
           QStandardItem *itemhost = new QStandardItem(host); // parent host
           QList<QStandardItem*> list;
           list << itemname << itemhost;

           pname->appendRow(list);
           hostmap.insert(host, indexFromItem(itemname));
       }
   }
}