Switch to unified view

a/upmpd/mpdcli.cxx b/upmpd/mpdcli.cxx
...
...
37
{
37
{
38
    regcomp(&m_tpuexpr, "^[[:alpha:]]+://.+", REG_EXTENDED|REG_NOSUB);
38
    regcomp(&m_tpuexpr, "^[[:alpha:]]+://.+", REG_EXTENDED|REG_NOSUB);
39
    if (!openconn()) {
39
    if (!openconn()) {
40
        return;
40
        return;
41
    }
41
    }
42
    m_have_addtagid = checkForCommand("addtagid");
43
42
    m_ok = true;
44
    m_ok = true;
43
    m_ok = updStatus();
45
    m_ok = updStatus();
44
}
46
}
45
47
46
MPDCli::~MPDCli()
48
MPDCli::~MPDCli()
...
...
200
    if (err != 0)
202
    if (err != 0)
201
        m_stat.errormessage.assign(err);
203
        m_stat.errormessage.assign(err);
202
204
203
    mpd_status_free(mpds);
205
    mpd_status_free(mpds);
204
    return true;
206
    return true;
207
}
208
209
bool MPDCli::checkForCommand(const string& cmdname)
210
{
211
    LOGDEB1("MPDCli::checkForCommand: " << cmdname << endl);
212
    bool found = false;
213
214
    RETRY_CMD(mpd_send_allowed_commands(M_CONN));
215
    struct mpd_pair *rep;
216
    do {
217
        rep = mpd_recv_command_pair(M_CONN);
218
        if (rep) {
219
            //LOGDEB("MPDCli::checkForCommand: name " <<  rep->name << 
220
            //       " value " << rep->value << endl);
221
            found = !cmdname.compare(rep->value);
222
            mpd_return_pair(M_CONN, rep);
223
            if (found)
224
                break;
225
        }
226
    } while (rep);
227
228
    if (!mpd_response_finish(M_CONN)) {
229
        LOGERR("MPDCli::checkForCommand: mpd_response_finish failed" << endl);
230
    }
231
232
    return found;
205
}
233
}
206
234
207
bool MPDCli::statSong(UpSong& upsong, int pos, bool isid)
235
bool MPDCli::statSong(UpSong& upsong, int pos, bool isid)
208
{
236
{
209
    //LOGDEB("MPDCli::statSong. isid " << isid << " val " << pos << endl);
237
    //LOGDEB("MPDCli::statSong. isid " << isid << " val " << pos << endl);
...
...
431
        return false;
459
        return false;
432
    RETRY_CMD(mpd_run_single(M_CONN, on));
460
    RETRY_CMD(mpd_run_single(M_CONN, on));
433
    return true;
461
    return true;
434
}
462
}
435
463
464
bool MPDCli::send_tag(const char *cid, int tag, const string& data)
465
{
466
    if (!mpd_send_command(M_CONN, "addtagid", cid, 
467
                          mpd_tag_name(mpd_tag_type(tag)),
468
                          data.c_str(), NULL)) {
469
        LOGERR("MPDCli::send_tag: mpd_send_command failed" << endl);
470
        return false;
471
    }
472
473
    return mpd_response_finish(M_CONN);
474
}
475
476
static const string upmpdcli_comment("client=upmpdcli;");
477
478
bool MPDCli::send_tag_data(int id, const UpSong& meta)
479
{
480
    if (!m_have_addtagid)
481
        return false;
482
483
    char cid[30];
484
    sprintf(cid, "%d", id);
485
486
    if (!send_tag(cid, MPD_TAG_ARTIST, meta.artist))
487
        return false;
488
    if (!send_tag(cid, MPD_TAG_ALBUM, meta.album))
489
        return false;
490
    if (!send_tag(cid, MPD_TAG_TITLE, meta.title))
491
        return false;
492
    if (!send_tag(cid, MPD_TAG_TRACK, meta.tracknum))
493
        return false;
494
    if (!send_tag(cid, MPD_TAG_COMMENT, upmpdcli_comment))
495
        return false;
496
    return true;
497
}
498
436
int MPDCli::insert(const string& uri, int pos)
499
int MPDCli::insert(const string& uri, int pos, const UpSong& meta)
437
{
500
{
438
    LOGDEB("MPDCli::insert at :" << pos << " uri " << uri << endl);
501
    LOGDEB("MPDCli::insert at :" << pos << " uri " << uri << endl);
439
    if (!ok())
502
    if (!ok())
440
        return -1;
503
        return -1;
441
504
442
    if (!updStatus())
505
    if (!updStatus())
443
        return -1;
506
        return -1;
444
507
445
    int id;
508
    int id;
446
    RETRY_CMD((id=mpd_run_add_id_to(M_CONN, uri.c_str(), (unsigned)pos))!=-1);
509
    RETRY_CMD((id=mpd_run_add_id_to(M_CONN, uri.c_str(), (unsigned)pos))!=-1);
510
    
511
    if (m_have_addtagid)
512
        send_tag_data(id, meta);
447
513
448
    return id;
514
    return id;
449
}
515
}
450
516
451
int MPDCli::insertAfterId(const string& uri, int id)
517
int MPDCli::insertAfterId(const string& uri, int id, const UpSong& meta)
452
{
518
{
453
    LOGDEB("MPDCli::insertAfterId: id " << id << " uri " << uri << endl);
519
    LOGDEB("MPDCli::insertAfterId: id " << id << " uri " << uri << endl);
454
    if (!ok())
520
    if (!ok())
455
        return -1;
521
        return -1;
456
522
457
    if (!updStatus())
523
    if (!updStatus())
458
        return -1;
524
        return -1;
459
525
460
    // id == 0 means insert at start
526
    // id == 0 means insert at start
461
    if (id == 0) {
527
    if (id == 0) {
462
        return insert(uri, 0);
528
        return insert(uri, 0, meta);
463
    }
529
    }
464
530
465
    vector<mpd_song*> songs;
531
    vector<mpd_song*> songs;
466
    if (!getQueueSongs(songs)) {
532
    if (!getQueueSongs(songs)) {
467
        return false;
533
        return false;
468
    }
534
    }
469
    int newid = -1;
535
    int newid = -1;
470
    for (unsigned int pos = 0; pos < songs.size(); pos++) {
536
    for (unsigned int pos = 0; pos < songs.size(); pos++) {
471
        unsigned int qid = mpd_song_get_id(songs[pos]);
537
        unsigned int qid = mpd_song_get_id(songs[pos]);
472
        if (qid == (unsigned int)id || pos == songs.size() -1) {
538
        if (qid == (unsigned int)id || pos == songs.size() -1) {
473
            newid = insert(uri, pos+1);
539
            newid = insert(uri, pos+1, meta);
474
            break;
540
            break;
475
        }
541
        }
476
    }
542
    }
477
    freeSongs(songs);
543
    freeSongs(songs);
478
    return newid;
544
    return newid;