Ticket #355: mythmusic_comma.patch

File mythmusic_comma.patch, 4.9 KB (added by wseltzer@…, 19 years ago)

On top of the previous patch, search multiple fields separated by commas

  • mythmusic/search.cpp

    old new  
    7575
    7676    bool substringSearch = true;
    7777    bool isNumber = false;
     78    QString testString;
    7879    searchText.toULongLong(&isNumber);
     80 
     81  if ( !isNumber ) {
     82      testString=searchText;
     83      testString.replace(">","");
     84      testString.toULongLong(&isNumber);
     85    }
    7986
    8087    char *mapping[] = {
    8188        "[0 ]",   "1",       "[2abc]", "[3def]", "[4ghi]", "[5jkl]",
    8289        "[6mno]", "[7pqrs]", "[8tuv]", "[9wxyz]"
    8390    };
    84 
     91   
    8592    if (isNumber) {
    86         // Input contains only digits. Enable digit-to-char mapping mode
    87         for (int i = 0; i < 10; i++) {
    88             char c = '0' + i;
    89             searchText.replace(QChar(c), QString(mapping[i]));
    90         }
     93      // Input contains only digits. Enable digit-to-char mapping mode
     94      for (int i = 0; i < 10; i++) {
     95        char c = '0' + i;
     96        searchText.replace(QChar(c), QString(mapping[i]));
     97      }
    9198      substringSearch = false;
    9299    }
    93 
     100   
    94101    listbox->clear();
    95 
     102   
    96103    MSqlQuery query(MSqlQuery::InitCon());
    97104
    98105    QString queryString("SELECT filename,artist,album,title,intid "
    99106                        "FROM musicmetadata ");
    100107
    101108    if (!searchText.isEmpty())
    102     {
    103         if (substringSearch)
    104         {
    105             whereClause = "WHERE filename LIKE '%" + searchText + "%'"
    106                           "   OR artist   LIKE '%" + searchText + "%'"
    107                           "   OR album    LIKE '%" + searchText + "%'"
    108                           "   OR title    LIKE '%" + searchText + "%'"
    109                           ";";
    110         }
    111         else
    112         {
    113             // regexp search
    114             whereClause = "WHERE filename REGEXP '" + searchText +
    115                           "'  OR artist   REGEXP '" + searchText +
    116                           "'  OR album    REGEXP '" + searchText +
    117                           "'  OR title    REGEXP '" + searchText +
    118                           "';";
    119         }
    120     }
    121 
     109      {
     110        if (substringSearch) //alphanumeric
     111          {
     112            QStringList list = QStringList::split(",", searchText);
     113            whereClause="";
     114            for (uint x = 0; x < list.count(); x++)
     115              {
     116                testString = list[x].stripWhiteSpace();
     117                if ( x == 0 ) {
     118                  whereClause += "WHERE (";
     119                }
     120                else  //                  if (x > 0)
     121                  {
     122                    whereClause += " AND ( ";
     123                  }
     124                  whereClause += "artist   LIKE '%" + testString + "%'"
     125                           "   OR album    LIKE '%" + testString + "%'"
     126                           "   OR title    LIKE '%" + testString + "%'"
     127                           "   OR filename LIKE '%" + testString + "%'"
     128                           ")";
     129              }
     130            whereClause += ";";
     131            VERBOSE(VB_ALL, QString("alpha whereClause " + whereClause ));
     132          }
     133        else //numbers
     134          {
     135            QStringList list = QStringList::split(">", searchText);
     136            whereClause="";
     137            for (uint x = 0; x < list.count(); x++)
     138              {
     139                testString = list[x].stripWhiteSpace();
     140                if ( x == 0 ) {
     141                  whereClause += "WHERE (";
     142                } else {
     143                  whereClause += " AND ( ";
     144                }
     145                whereClause += " artist  REGEXP '" + testString +
     146                         "'   OR album   REGEXP '" + testString +
     147                         "'   OR title   REGEXP '" + testString +
     148                         "'   OR filename REGEXP '" + testString +
     149                         "')";
     150              }
     151            whereClause += ";";
     152            VERBOSE(VB_ALL, QString("numeric whereClause " + whereClause ));
     153          }
     154      }
    122155    unsigned int matchCount = 0;
    123156
    124157    queryString += whereClause;
    125 
    126158    query.prepare(queryString);
    127159
    128160    if (query.exec())
     
    132164            while (query.next())
    133165            {
    134166                // Strip path from filename
    135                 QString fileName(query.value(0).toString());
    136                 fileName.replace(QRegExp(".*/"), "");
     167                // QString fileName(query.value(0).toString());
     168                // fileName.replace(QRegExp(".*/"), "");
    137169
    138                 // Append artist/title/album info... ok this is ugly :(
    139                 QString text(fileName + " [ " +
    140                           query.value(1).toString() + "/" +
    141                           query.value(2).toString() + "/" +
    142                           query.value(3).toString() + "]");
    143                
    144                 // Insert item into listbox, including song identifier
    145                 listbox->insertItem(
     170              QString aComposer(query.value(1).toString());
     171              // cut off composer's first name
     172              aComposer.replace(QRegExp(",.*"),"");
     173             
     174              QString aTitle(query.value(2).toString());
     175              // truncate Title at 30, cut off any trailing whitespace or ,
     176              aTitle.truncate( 30 );
     177              aTitle.replace(QRegExp(",*\\s*$"),"");
     178             
     179              // Append artist/title/album info... ok this is ugly :(
     180              QString text(aComposer + ": " +
     181                           aTitle + "; " +
     182                           query.value(3).toString() );
     183             
     184              // Insert item into listbox, including song identifier
     185              listbox->insertItem(
    146186                    new SearchListBoxItem(QString::fromUtf8(text),
    147187                                          query.value(4).toUInt())
    148188                    );