[PATCH] Re: [mythtv] Recorded Programs menu

Jim Radford mythtv-dev@snowman.net
Mon, 11 Nov 2002 12:03:50 -0800


On Mon, Nov 11, 2002 at 11:49:57AM -0600, rob wrote:
> I think it might be nice to consolidate the "Watch A Recording" and 
> "Delete Recordings" menus into one "Recorded Programs" menu, where you 
> can watch or delete a previously recorded show.  I have found myself 

I totally agree.  This works for me.  Right now space plays and
return deletes.  This is clearly suboptimal. :-) Someone who knows QT
might want to figure out how to change MyListView::keyPressEvent to
emit dPressed.

-Jim

Index: mythfrontend.pro
===================================================================
RCS file: /var/lib/cvs/MC/programs/mythfrontend/mythfrontend.pro,v
retrieving revision 1.21
diff -u -r1.21 mythfrontend.pro
--- mythfrontend.pro    29 Oct 2002 04:58:52 -0000      1.21
+++ mythfrontend.pro    11 Nov 2002 19:55:50 -0000
@@ -28,8 +28,8 @@
 TARGETDEPS += ../../libs/libavcodec/libavcodec.a

 # Input
-HEADERS += scheduler.h playbackbox.h deletebox.h programlistitem.h
+HEADERS += scheduler.h playbackbox.h programlistitem.h
 HEADERS += viewscheduled.h

-SOURCES += main.cpp scheduler.cpp playbackbox.cpp deletebox.cpp
+SOURCES += main.cpp scheduler.cpp playbackbox.cpp
 SOURCES += programlistitem.cpp viewscheduled.cpp
Index: main.cpp
===================================================================
RCS file: /var/lib/cvs/MC/programs/mythfrontend/main.cpp,v
retrieving revision 1.41
diff -u -r1.41 main.cpp
--- main.cpp	30 Oct 2002 18:20:36 -0000	1.41
+++ main.cpp	11 Nov 2002 19:48:15 -0000
@@ -10,7 +10,6 @@
 #include "tv.h"
 #include "scheduler.h"
 #include "playbackbox.h"
-#include "deletebox.h"
 #include "viewscheduled.h"
 
 #include "libmyth/themedmenu.h"
@@ -51,18 +50,6 @@
     return 0;
 }
 
-int startDelete(MythContext *context)
-{
-    QSqlDatabase *db = QSqlDatabase::database();
-    DeleteBox delbox(context, tvList.begin().data(), db);
-   
-    delbox.Show();
-    
-    delbox.exec();
-
-    return 0;
-}
-
 void startTV(void)
 {
     TV *tv = tvList.begin().data();
@@ -95,17 +82,15 @@
     Scheduler *sched = new Scheduler(db);
 
     bool asked = false;
-    int secsleft;
     TV *nexttv = NULL;
 
     ProgramInfo *nextRecording = NULL;
     QDateTime nextrectime;
-    QDateTime curtime;
     QDateTime lastupdate = QDateTime::currentDateTime().addDays(-1);
 
     while (1)
     {
-        curtime = QDateTime::currentDateTime();
+        QDateTime curtime = QDateTime::currentDateTime();
 
         if (sched->CheckForChanges() || 
             (lastupdate.date().day() != curtime.date().day()))
@@ -136,7 +121,7 @@
 
         if (nextRecording)
         {
-            secsleft = curtime.secsTo(nextrectime);
+            int secsleft = curtime.secsTo(nextrectime);
 
             //cout << secsleft << " seconds until " << nextRecording->title 
             //     << endl;
@@ -184,8 +169,6 @@
         startPlayback(context);
     else if (sel == "tv_schedule")
         startGuide(context);
-    else if (sel == "tv_delete")
-        startDelete(context);
     else if (sel == "tv_fix_conflicts")
         startManaged(context);
     else if (sel == "tv_setup")
Index: playbackbox.cpp
===================================================================
RCS file: /var/lib/cvs/MC/programs/mythfrontend/playbackbox.cpp,v
retrieving revision 1.30
diff -u -r1.30 playbackbox.cpp
--- playbackbox.cpp	31 Oct 2002 20:23:49 -0000	1.30
+++ playbackbox.cpp	11 Nov 2002 19:48:15 -0000
@@ -6,6 +6,7 @@
 #include <qsqldatabase.h>
 #include <qlistview.h>
 #include <qdatetime.h>
+#include <qprogressbar.h>
 #include <qapplication.h>
 #include <qtimer.h>
 #include <qimage.h>
@@ -18,8 +19,9 @@
 #include "NuppelVideoPlayer.h"
 #include "yuv2rgb.h"
 
-#include "libmyth/mythcontext.h"
 #include "libmyth/programinfo.h"
+#include "libmyth/dialogbox.h"
+#include "libmyth/mythcontext.h"
 
 PlaybackBox::PlaybackBox(MythContext *context, TV *ltv, QSqlDatabase *ldb, 
                          QWidget *parent, const char *name)
@@ -42,28 +44,30 @@
             QFont::Bold));
     setCursor(QCursor(Qt::BlankCursor));
 
-    QVBoxLayout *vbox = new QVBoxLayout(this, (int)(20 * wmult));
+    QVBoxLayout *vbox = new QVBoxLayout(this, (int)(15 * wmult));
 
-    QLabel *label = new QLabel("Select a recording to view:", this);
+    QLabel *label = new QLabel("View and Delete:", this);
     vbox->addWidget(label);
 
-    QListView *listview = new QListView(this);
-
+    listview = new MyListView(this);
     listview->addColumn("Date");
     listview->addColumn("Title");
+    listview->addColumn("Size");
  
-    listview->setColumnWidth(0, (int)(220 * wmult)); 
-    listview->setColumnWidth(1, (int)(520 * wmult));
-    listview->setColumnWidthMode(0, QListView::Manual);
-    listview->setColumnWidthMode(1, QListView::Manual);
+    listview->setColumnWidth(0, (int)(200 * wmult)); 
+    listview->setColumnWidth(1, (int)(455 * wmult));
+    listview->setColumnWidth(2, (int)(90 * wmult));
+    //listview->setColumnWidthMode(0, QListView::Manual);
+    //listview->setColumnWidthMode(1, QListView::Manual);
+    //listview->setColumnWidthMode(2, QListView::Manual);
 
     listview->setSorting(-1, false);
     listview->setAllColumnsShowFocus(TRUE);
 
     connect(listview, SIGNAL(returnPressed(QListViewItem *)), this,
-            SLOT(selected(QListViewItem *)));
+            SLOT(remove(QListViewItem *)));
     connect(listview, SIGNAL(spacePressed(QListViewItem *)), this,
-            SLOT(selected(QListViewItem *))); 
+            SLOT(play(QListViewItem *))); 
     connect(listview, SIGNAL(selectionChanged(QListViewItem *)), this,
             SLOT(changed(QListViewItem *)));
 
@@ -74,8 +78,8 @@
     ProgramListItem *item = NULL;
    
     thequery = "SELECT chanid,starttime,endtime,title,subtitle,description "
-               "FROM recorded ORDER BY starttime;";
- 
+               "FROM recorded ORDER BY starttime DESC;";
+
     query = db->exec(thequery);
 
     if (query.isActive() && query.numRowsAffected() > 0)
@@ -119,11 +123,11 @@
             else
             {
                 proginfo->chanstr = "#" + proginfo->chanid;
-                proginfo->channame = "#" + proginfo->chanid;
-                proginfo->chansign = "#" + proginfo->chanid;
+                proginfo->channame = proginfo->chanstr;
+                proginfo->chansign = proginfo->chanstr;
             }
 
-            item = new ProgramListItem(context, listview, proginfo, 0, tv, 
+            item = new ProgramListItem(context, listview, proginfo, 1, tv, 
                                        fileprefix);
         }
     }
@@ -132,7 +136,7 @@
         // TODO: no recordings
     }
    
-    listview->setFixedHeight((int)(300 * hmult));
+    listview->setFixedHeight((int)(225 * hmult));
 
     QHBoxLayout *hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
 
@@ -168,8 +172,8 @@
     grid->setColStretch(1, 1);
     grid->setRowStretch(4, 1);
 
-    if (m_context->GetNumSetting("GeneratePreviewPixmap") == 1 ||
-        m_context->GetNumSetting("PlaybackPreview") == 1)
+    if (context->GetNumSetting("GeneratePreviewPixmap") == 1 ||
+        context->GetNumSetting("PlaybackPreview") == 1)
     {
         QPixmap temp((int)(160 * wmult), (int)(120 * hmult));
 
@@ -181,6 +185,13 @@
     else
         pixlabel = NULL;
 
+    freespace = new QLabel(" ", this);
+    vbox->addWidget(freespace);
+    
+    progressbar = new QProgressBar(this);
+    UpdateProgressBar();
+    vbox->addWidget(progressbar);
+
     nvp = NULL;
     timer = new QTimer(this);
 
@@ -264,13 +275,22 @@
 {
     killPlayer();
 
+    if (!title)
+        return;
+
     ProgramListItem *pgitem = (ProgramListItem *)lvitem;
     if (!pgitem)
+    {
+        title->setText("");
+        date->setText("");
+        chan->setText("");
+        subtitle->setText("");
+        description->setText("");
+        if (pixlabel)
+            pixlabel->setPixmap(QPixmap(0, 0));
         return;
+    }
    
-    if (!title)
-        return;
-
     ProgramInfo *rec = pgitem->getProgramInfo();
 
     if (m_context->GetNumSetting("PlaybackPreview") == 1)
@@ -321,7 +341,26 @@
     timer->start(1000 / 30);
 }
 
-void PlaybackBox::selected(QListViewItem *lvitem)
+static void *SpawnDelete(void *param)
+{   
+    QString *filenameptr = (QString *)param;
+    QString filename = *filenameptr;
+
+    unlink(filename.ascii());
+
+    filename += ".png";
+    unlink(filename.ascii());
+
+    filename = *filenameptr;
+    filename += ".bookmark";
+    unlink(filename.ascii());
+
+    delete filenameptr;
+
+    return NULL;
+}   
+
+void PlaybackBox::play(QListViewItem *lvitem)
 {
     killPlayer();
 
@@ -336,6 +375,156 @@
 
     startPlayer(rec);
     timer->start(1000 / 30);
+}
+
+void PlaybackBox::remove(QListViewItem *lvitem)
+{
+    killPlayer();
+	
+    ProgramListItem *pgitem = (ProgramListItem *)lvitem;
+    ProgramInfo *rec = pgitem->getProgramInfo();
+
+    QString message = "Are you sure you want to delete:<br><br>";
+    message += rec->title;
+    message += "<br>";
+    
+    QDateTime startts = rec->startts;
+    QDateTime endts = rec->endts;
+
+    QString dateformat = m_context->GetSetting("DateFormat");
+    if (dateformat == "")
+        dateformat = "ddd MMMM d";
+    QString timeformat = m_context->GetSetting("TimeFormat");
+    if (timeformat == "")
+        timeformat = "h:mm AP";
+
+    QString timedate = endts.date().toString(dateformat) + QString(", ") +
+                       startts.time().toString(timeformat) + QString(" - ") +
+                       endts.time().toString(timeformat);
+
+    message += timedate;
+    message += "<br>";
+    if (rec->subtitle != "(null)")
+        message += rec->subtitle;
+    message += "<br>";
+    if (rec->description != "(null)")
+        message += rec->description;
+
+    message += "<br><br>It will be gone forever.";
+
+    DialogBox diag(m_context, message);
+
+    diag.AddButton("Yes, get rid of it");
+    diag.AddButton("No, keep it, I changed my mind");
+
+    diag.Show();
+
+    int result = diag.exec();
+
+    if (result == 1)
+    {
+        QString filename = rec->GetRecordFilename(fileprefix);
+
+        QSqlQuery query;
+        QString thequery;
+
+        QString startts = rec->startts.toString("yyyyMMddhhmm");
+        startts += "00";
+        QString endts = rec->endts.toString("yyyyMMddhhmm");
+        endts += "00";
+
+        thequery = QString("DELETE FROM recorded WHERE chanid = %1 AND title "
+                           "= \"%2\" AND starttime = %3 AND endtime = %4;")
+                           .arg(rec->chanid).arg(rec->title).arg(startts)
+                           .arg(endts);
+
+        query = db->exec(thequery);
+        if (!query.isActive())
+        {
+            cerr << "DB Error: recorded program deletion failed, SQL query "
+                 << "was:" << endl;
+            cerr << thequery << endl;
+        }
+
+        QString *fileptr = new QString(filename);
+
+        pthread_t deletethread;
+        pthread_attr_t attr;
+        pthread_attr_init(&attr);
+        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+        pthread_create(&deletethread, &attr, SpawnDelete, fileptr);
+
+        if (lvitem->itemBelow())
+        {
+            listview->setCurrentItem(lvitem->itemBelow());
+            listview->setSelected(lvitem->itemBelow(), true);
+        }
+        else if (lvitem->itemAbove())
+        {
+            listview->setCurrentItem(lvitem->itemAbove());
+            listview->setSelected(lvitem->itemAbove(), true);
+        }
+        else
+            changed(NULL);
+
+        delete lvitem;
+        UpdateProgressBar();
+    }    
+    else if (m_context->GetNumSetting("PlaybackPreview") == 1)
+        startPlayer(rec);
+
+    setActiveWindow();
+    raise();
+
+    timer->start(1000 / 30);
+}
+
+void PlaybackBox::GetFreeSpaceStats(int &totalspace, int &usedspace)
+{
+    QString command;
+    command.sprintf("df -k -P %s", fileprefix.ascii());
+
+    FILE *file = popen(command.ascii(), "r");
+
+    if (!file)
+    {
+        totalspace = -1;
+        usedspace = -1;
+    }
+    else
+    {
+        char buffer[1024];
+        fgets(buffer, 1024, file);
+        fgets(buffer, 1024, file);
+
+        char dummy[1024];
+        int dummyi;
+        sscanf(buffer, "%s %d %d %d %s %s\n", dummy, &totalspace, &usedspace,
+               &dummyi, dummy, dummy);
+
+        totalspace /= 1000;
+        usedspace /= 1000; 
+        pclose(file);
+    }
+}
+ 
+void PlaybackBox::UpdateProgressBar(void)
+{
+    int total, used;
+    GetFreeSpaceStats(total, used);
+
+    QString usestr;
+    char text[128];
+    sprintf(text, "Storage: %d,%03d MB used out of %d,%03d MB total", 
+            used / 1000, used % 1000, 
+            total / 1000, total % 1000);
+
+    usestr = text;
+
+    freespace->setText(usestr);
+    progressbar->setTotalSteps(total);
+    progressbar->setProgress(used);
 }
 
 void PlaybackBox::timeout(void)
Index: playbackbox.h
===================================================================
RCS file: /var/lib/cvs/MC/programs/mythfrontend/playbackbox.h,v
retrieving revision 1.8
diff -u -r1.8 playbackbox.h
--- playbackbox.h	31 Oct 2002 20:23:49 -0000	1.8
+++ playbackbox.h	11 Nov 2002 19:48:15 -0000
@@ -8,7 +8,9 @@
 
 class QSqlDatabase;
 class QListViewItem;
+class MyListView;
 class QLabel;
+class QProgressBar;
 class TV;
 class NuppelVideoPlayer;
 class RingBuffer;
@@ -27,11 +29,15 @@
     void Show();
   
   protected slots:
-    void selected(QListViewItem *);
+    void remove(QListViewItem *);
+    void play(QListViewItem *);
     void changed(QListViewItem *);
     void timeout(void);
 
   private:
+    void GetFreeSpaceStats(int &totalspace, int &usedspace); 
+    void UpdateProgressBar(void);
+
     QSqlDatabase *db;
     TV *tv;
 
@@ -44,6 +50,11 @@
     QLabel *chan;
     QLabel *pixlabel;
 
+    MyListView *listview;
+
+    QLabel *freespace;
+    QProgressBar *progressbar;
+ 
     float wmult, hmult;
     int descwidth;