~ubuntu-branches/ubuntu/trusty/pianobar/trusty

« back to all changes in this revision

Viewing changes to src/ui.c

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2012-05-06 14:24:34 UTC
  • mfrom: (1.3.9)
  • Revision ID: package-import@ubuntu.com-20120506142434-74kwucnyp97msxdi
Tags: 2012.05.06-1
* New upstream version.
  - JSON api support (closes: #670483, LP: #988395)

Show diffs side-by-side

added added

removed removed

Lines of Context:
137
137
 */
138
138
static WaitressReturn_t BarPianoHttpRequest (WaitressHandle_t *waith,
139
139
                PianoRequest_t *req) {
140
 
        waith->extraHeaders = "Content-Type: text/xml\r\n";
 
140
        waith->extraHeaders = "Content-Type: text/plain\r\n";
141
141
        waith->postData = req->postData;
142
142
        waith->method = WAITRESS_METHOD_POST;
143
143
        waith->url.path = req->urlPath;
185
185
                *pRet = PianoResponse (&app->ph, &req);
186
186
                if (*pRet != PIANO_RET_CONTINUE_REQUEST) {
187
187
                        /* checking for request type avoids infinite loops */
188
 
                        if (*pRet == PIANO_RET_AUTH_TOKEN_INVALID &&
 
188
                        if (*pRet == PIANO_RET_P_INVALID_AUTH_TOKEN &&
189
189
                                        type != PIANO_REQUEST_LOGIN) {
190
190
                                /* reauthenticate */
191
191
                                PianoReturn_t authpRet;
330
330
 
331
331
/*      let user pick one station
332
332
 *      @param app handle
 
333
 *      @param stations that should be listed
333
334
 *      @param prompt string
334
335
 *      @param called if input was not a number
 
336
 *      @param auto-select if only one station remains after filtering
335
337
 *      @return pointer to selected station or NULL
336
338
 */
337
339
PianoStation_t *BarUiSelectStation (BarApp_t *app, PianoStation_t *stations,
338
 
                const char *prompt, BarUiSelectStationCallback_t callback) {
 
340
                const char *prompt, BarUiSelectStationCallback_t callback,
 
341
                bool autoselect) {
339
342
        PianoStation_t **sortedStations = NULL, *retStation = NULL;
340
 
        size_t stationCount, i;
 
343
        size_t stationCount, i, lastDisplayed, displayCount;
341
344
        char buf[100];
342
345
 
343
346
        if (stations == NULL) {
352
355
                        app->settings.sortOrder);
353
356
 
354
357
        do {
 
358
                displayCount = 0;
355
359
                for (i = 0; i < stationCount; i++) {
356
360
                        const PianoStation_t *currStation = sortedStations[i];
357
361
                        /* filter stations */
361
365
                                                currStation->isQuickMix ? 'Q' : ' ',
362
366
                                                !currStation->isCreator ? 'S' : ' ',
363
367
                                                currStation->name);
 
368
                                ++displayCount;
 
369
                                lastDisplayed = i;
364
370
                        }
365
371
                }
366
372
 
367
373
                BarUiMsg (&app->settings, MSG_QUESTION, prompt);
368
 
                if (BarReadlineStr (buf, sizeof (buf), &app->input,
369
 
                                BAR_RL_DEFAULT) == 0) {
370
 
                        free (sortedStations);
371
 
                        return NULL;
372
 
                }
373
 
 
374
 
                if (isnumeric (buf)) {
375
 
                        unsigned long selected = strtoul (buf, NULL, 0);
376
 
                        if (selected < stationCount) {
377
 
                                retStation = sortedStations[selected];
378
 
                        }
379
 
                }
380
 
 
381
 
                /* hand over buffer to external function if it was not a station number */
382
 
                if (retStation == NULL && callback != NULL) {
383
 
                        callback (app, buf);
 
374
                if (autoselect && displayCount == 1 && stationCount != 1) {
 
375
                        /* auto-select last remaining station */
 
376
                        BarUiMsg (&app->settings, MSG_NONE, "%i\n", lastDisplayed);
 
377
                        retStation = sortedStations[lastDisplayed];
 
378
                } else {
 
379
                        if (BarReadlineStr (buf, sizeof (buf), &app->input,
 
380
                                        BAR_RL_DEFAULT) == 0) {
 
381
                                break;
 
382
                        }
 
383
 
 
384
                        if (isnumeric (buf)) {
 
385
                                unsigned long selected = strtoul (buf, NULL, 0);
 
386
                                if (selected < stationCount) {
 
387
                                        retStation = sortedStations[selected];
 
388
                                }
 
389
                        }
 
390
 
 
391
                        /* hand over buffer to external function if it was not a station number */
 
392
                        if (retStation == NULL && callback != NULL) {
 
393
                                callback (app, buf);
 
394
                        }
384
395
                }
385
396
        } while (retStation == NULL);
386
397
 
473
484
 *      @return musicId or NULL on abort/error
474
485
 */
475
486
char *BarUiSelectMusicId (BarApp_t *app, PianoStation_t *station,
476
 
                PianoSong_t *similarSong, const char *msg) {
 
487
                const char *msg) {
477
488
        char *musicId = NULL;
478
489
        char lineBuf[100], selectBuf[2];
479
490
        PianoSearchResult_t searchResult;
483
494
        BarUiMsg (&app->settings, MSG_QUESTION, msg);
484
495
        if (BarReadlineStr (lineBuf, sizeof (lineBuf), &app->input,
485
496
                        BAR_RL_DEFAULT) > 0) {
486
 
                if (strcmp ("?", lineBuf) == 0 && station != NULL &&
487
 
                                similarSong != NULL) {
488
 
                        PianoReturn_t pRet;
489
 
                        WaitressReturn_t wRet;
490
 
                        PianoRequestDataGetSeedSuggestions_t reqData;
491
 
 
492
 
                        reqData.station = station;
493
 
                        reqData.musicId = similarSong->musicId;
494
 
                        reqData.max = 20;
495
 
 
496
 
                        BarUiMsg (&app->settings, MSG_INFO, "Receiving suggestions... ");
497
 
                        if (!BarUiPianoCall (app, PIANO_REQUEST_GET_SEED_SUGGESTIONS,
498
 
                                        &reqData, &pRet, &wRet)) {
499
 
                                return NULL;
500
 
                        }
501
 
                        memcpy (&searchResult, &reqData.searchResult, sizeof (searchResult));
502
 
                } else {
503
 
                        PianoReturn_t pRet;
504
 
                        WaitressReturn_t wRet;
505
 
                        PianoRequestDataSearch_t reqData;
506
 
 
507
 
                        reqData.searchStr = lineBuf;
508
 
 
509
 
                        BarUiMsg (&app->settings, MSG_INFO, "Searching... ");
510
 
                        if (!BarUiPianoCall (app, PIANO_REQUEST_SEARCH, &reqData, &pRet,
511
 
                                        &wRet)) {
512
 
                                return NULL;
513
 
                        }
514
 
                        memcpy (&searchResult, &reqData.searchResult, sizeof (searchResult));
 
497
                PianoReturn_t pRet;
 
498
                WaitressReturn_t wRet;
 
499
                PianoRequestDataSearch_t reqData;
 
500
 
 
501
                reqData.searchStr = lineBuf;
 
502
 
 
503
                BarUiMsg (&app->settings, MSG_INFO, "Searching... ");
 
504
                if (!BarUiPianoCall (app, PIANO_REQUEST_SEARCH, &reqData, &pRet,
 
505
                                &wRet)) {
 
506
                        return NULL;
515
507
                }
 
508
                memcpy (&searchResult, &reqData.searchResult, sizeof (searchResult));
 
509
 
516
510
                BarUiMsg (&app->settings, MSG_NONE, "\r");
517
511
                if (searchResult.songs != NULL &&
518
512
                                searchResult.artists != NULL) {
709
703
        BarUiCustomFormat (outstr, sizeof (outstr), settings->npStationFormat,
710
704
                        "ni", vals);
711
705
        BarUiAppendNewline (outstr, sizeof (outstr));
712
 
        BarUiMsg (settings, MSG_PLAYING, outstr);
 
706
        BarUiMsg (settings, MSG_PLAYING, "%s", outstr);
713
707
}
714
708
 
715
709
/*      Print song infos (artist, title, album, loved)
729
723
        BarUiCustomFormat (outstr, sizeof (outstr), settings->npSongFormat,
730
724
                        "talr@su", vals);
731
725
        BarUiAppendNewline (outstr, sizeof (outstr));
732
 
        BarUiMsg (settings, MSG_PLAYING, outstr);
 
726
        BarUiMsg (settings, MSG_PLAYING, "%s", outstr);
733
727
}
734
728
 
735
729
/*      Print list of songs
756
750
                        BarUiCustomFormat (outstr, sizeof (outstr), settings->listSongFormat,
757
751
                                        "iatr", vals);
758
752
                        BarUiAppendNewline (outstr, sizeof (outstr));
759
 
                        BarUiMsg (settings, MSG_LIST, outstr);
 
753
                        BarUiMsg (settings, MSG_LIST, "%s", outstr);
760
754
                }
761
755
                i++;
762
756
                song = song->next;