summaryrefslogblamecommitdiffstats
path: root/src/cardwidget.cc
blob: 5755d46c226565a1c0fe01b428f43f1c68d3d2ab (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
















                                                                      
                                                                      

    



                    

                       



                 
                    

                                         

                                                                                                                                    
                                                                                                                                                




                                
 
                         
                           




                                                     
                                              
                             

              
     

 
                                                    
 

















                                                             

                    
                                                                                                              


                                                                                             
 
                          

 
                                              


                 
                     
                                                                
 
 
                                                    


                 








                                                                               
                                    
                            
                               




                                                                                   
                         






















                                                               
                                     


                                      




                                                                                   









                                                                                   









                                                                           
 





                                                                 
 
/***
  This file is part of pavucontrol.

  Copyright 2006-2008 Lennart Poettering
  Copyright 2009 Colin Guthrie

  pavucontrol 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 2 of the License, or
  (at your option) any later version.

  pavucontrol 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 pavucontrol. If not, see <https://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "cardwidget.h"

#include <QDebug>
#include <QHash>
#include <QSet>

/*** CardWidget ***/
CardWidget::CardWidget(QWidget* parent) :
    QWidget(parent) {
    setupUi(this);
    connect(profileList, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &CardWidget::onProfileChange);
    connect(profileFlavorList, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &CardWidget::onProfileFlavorChange);
}


void CardWidget::prepareMenu() {
    int idx = 0;

    profileList->clear();
    /* Fill the ComboBox */
    for (auto k : profiles.keys()) {
        auto & profile = profiles[k];
        auto desc = profile.getProfileName();
        profileList->addItem(desc, k);
        if (profile.containsProfile(activeProfile)) {
            profileList->setCurrentIndex(idx);
            changeProfile(k);
        }
        ++idx;
    }
}

void CardWidget::changeProfile(const QString & name)
{
    auto &profile = profiles[name];
    auto old = profileFlavorList->currentText();
    profileFlavorList->clear();
    int idx = 0;
    for (auto & entry : profile.entries) {
        auto name = entry.getName();
        profileFlavorList->addItem(name, entry.id);
        if (name == old || entry.id == activeProfile) {
            profileFlavorList->setCurrentIndex(idx);
        }
        ++idx;
    }
    if (profileFlavorList->currentIndex() == -1) {
        profileFlavorList->setCurrentIndex(0);
    }
}

void CardWidget::changeProfileFlavor(const QByteArray & id) {
    pa_operation* o;

    if (!(o = pa_context_set_card_profile_by_index(get_context(), index, id.constData(), nullptr, nullptr))) {
        show_error(tr("pa_context_set_card_profile_by_index() failed").toUtf8().constData());
        return;
    }

    pa_operation_unref(o);
}

void CardWidget::onProfileChange(int active) {
    if (updating)
        return;

    if (active != -1)
        changeProfile(profileList->itemData(active).toString());
}

void CardWidget::onProfileFlavorChange(int active) {
    if (updating)
        return;

    if (active != -1)
        changeProfileFlavor(profileFlavorList->itemData(active).toByteArray());
}

QString ProfileGroup::getProfileName() {
    if (!name.isEmpty())
        return name;
    // TODO
    QHash<QString, int> counts;
    //qDebug() << "Entries" << this;
    for (auto n : entries) {
        //qDebug() << n.tokens;
        QSet<QString> tmp = n.tokens.toSet(); //(n.tokens.begin(), n.tokens.end());
        for (auto t : tmp) {
            counts[t]++;
        }
    }
    //qDebug() << counts;
    QSet<QString> todo = counts.keys().toSet();
    for (auto &pe : entries) {
        QMutableListIterator<QString> it(pe.tokens);
        while (it.hasNext()) {
            auto s = it.next();
            if (counts[s] == entries.size()) {
                it.remove();
                if (todo.remove(s)) {
                    if (!name.isEmpty()) {
                        name.append(QLatin1Char(' '));
                    }
                    name.append(s);
                }
            }
        }
    }
    return name;
}

void ProfileGroup::addEntry(const char *id, const char *name) {
    auto *pe = new ProfileEntry;
    bool paren = false;
    const char *pos = name, *tokenStart = name;
    //qDebug() << name << "is" << id;
    while (*pos != '\0') {
        if (!paren && isspace(*pos)) {
            if (pos > tokenStart) {
                pe->tokens.append(QString::fromUtf8(tokenStart, pos - tokenStart));
            }
            tokenStart = pos + 1;
        }
        if (paren && *pos == ')') {
            pe->tokens.append(QString::fromUtf8(tokenStart, pos - tokenStart + 1));
            paren = false;
            tokenStart = pos + 1;
        }
        if (tokenStart == pos && *pos == '(') {
            paren = true;
        }
        pos++;
    }
    if (pos > tokenStart) {
        pe->tokens.append(QString::fromUtf8(tokenStart, pos - tokenStart));
    }
    pe->id = id;
    this->entries.append(*pe);
    delete pe;
}

QString ProfileEntry::getName() const {
    return tokens.join(QLatin1String(" "));
}

bool ProfileGroup::containsProfile(const QByteArray &pro) const {
    for (const auto &e : this->entries) {
        if (e.id == pro)
            return true;
    }
    return false;
}