lib/python/Components/Ipkg.py: small fix for opkg
[enigma2.git] / lib / service / listboxservice.cpp
index 9d68a5e835aecb42301d88b3cde352750bd143c9..05aaf73148bbaed3857ddf005f112f406817c912 100644 (file)
@@ -3,28 +3,80 @@
 #include <lib/gdi/font.h>
 #include <lib/dvb/epgcache.h>
 #include <lib/dvb/pmt.h>
+#include <lib/python/connections.h>
 
-void eListboxServiceContent::setRoot(const eServiceReference &root)
+void eListboxServiceContent::addService(const eServiceReference &service, bool beforeCurrent)
 {
-       m_list.clear();
-       m_root = root;
-       
-       assert(m_service_center);
-       
-       ePtr<iListableService> lst;
-       if (m_service_center->list(m_root, lst))
-               eDebug("no list available!");
+       if (beforeCurrent && m_size)
+               m_list.insert(m_cursor, service);
        else
-               if (lst->getContent(m_list))
-                       eDebug("getContent failed");
+               m_list.push_back(service);
+       if (m_size++)
+       {
+               ++m_cursor_number;
+               if (m_listbox)
+                       m_listbox->entryAdded(m_cursor_number-1);
+       }
+       else
+       {
+               m_cursor = m_list.begin();
+               m_cursor_number=0;
+               m_listbox->entryAdded(0);
+       }
+}
+
+void eListboxServiceContent::removeCurrent()
+{
+       if (m_size && m_listbox)
+       {
+               if (m_cursor_number == --m_size)
+               {
+                       m_list.erase(m_cursor--);
+                       if (m_size)
+                       {
+                               --m_cursor_number;
+                               m_listbox->entryRemoved(m_cursor_number+1);
+                       }
+                       else
+                               m_listbox->entryRemoved(m_cursor_number);
+               }
+               else
+               {
+                       m_list.erase(m_cursor++);
+                       m_listbox->entryRemoved(m_cursor_number);
+               }
+       }
+}
 
+void eListboxServiceContent::FillFinished()
+{
        m_size = m_list.size();
        cursorHome();
-       
+
        if (m_listbox)
                m_listbox->entryReset();
 }
 
+void eListboxServiceContent::setRoot(const eServiceReference &root, bool justSet)
+{
+       m_list.clear();
+       m_root = root;
+
+       if (justSet)
+       {
+               m_lst=0;
+               return;
+       }
+       ASSERT(m_service_center);
+       
+       if (m_service_center->list(m_root, m_lst))
+               eDebug("no list available!");
+       else if (m_lst->getContent(m_list))
+               eDebug("getContent failed");
+
+       FillFinished();
+}
+
 void eListboxServiceContent::setCurrent(const eServiceReference &ref)
 {
        int index=0;
@@ -35,6 +87,8 @@ void eListboxServiceContent::setCurrent(const eServiceReference &ref)
                        m_cursor_number = index;
                        break;
                }
+       if (m_listbox)
+               m_listbox->moveSelectionTo(index);
 }
 
 void eListboxServiceContent::getCurrent(eServiceReference &ref)
@@ -72,6 +126,38 @@ int eListboxServiceContent::getNextBeginningWithChar(char c)
        return 0;
 }
 
+int eListboxServiceContent::getPrevMarkerPos()
+{
+       if (!m_listbox)
+               return 0;
+       list::iterator i(m_cursor);
+       int index = m_cursor_number;
+       while (index)
+       {
+               --i;
+               --index;
+               if (i->flags & eServiceReference::isMarker)
+                       break;
+       }
+       return index;
+}
+
+int eListboxServiceContent::getNextMarkerPos()
+{
+       if (!m_listbox)
+               return 0;
+       list::iterator i(m_cursor);
+       int index = m_cursor_number;
+       while (index < (m_size-1))
+       {
+               ++i;
+               ++index;
+               if (i->flags & eServiceReference::isMarker)
+                       break;
+       }
+       return index;
+}
+
 void eListboxServiceContent::initMarked()
 {
        m_marked.clear();
@@ -124,17 +210,18 @@ int eListboxServiceContent::lookupService(const eServiceReference &ref)
 
 void eListboxServiceContent::setVisualMode(int mode)
 {
+       for (int i=0; i < celElements; ++i)
+       {
+               m_element_position[i] = eRect();
+               m_element_font[i] = 0;
+       }
+
        m_visual_mode = mode;
-       
+
        if (m_visual_mode == visModeSimple)
        {
                m_element_position[celServiceName] = eRect(ePoint(0, 0), m_itemsize);
-               m_element_font[celServiceName] = new gFont("Arial", 23);
-               m_element_position[celServiceNumber] = eRect();
-               m_element_font[celServiceNumber] = 0;
-               m_element_position[celIcon] = eRect();
-               m_element_position[celServiceInfo] = eRect();
-               m_element_font[celServiceInfo] = 0;
+               m_element_font[celServiceName] = new gFont("Regular", 23);
        }
 }
 
@@ -150,12 +237,19 @@ void eListboxServiceContent::setElementFont(int element, gFont *font)
                m_element_font[element] = font;
 }
 
+void eListboxServiceContent::setPixmap(int type, ePtr<gPixmap> &pic)
+{
+       if ((type >=0) && (type < picElements))
+               m_pixmaps[type] = pic;
+}
+
 void eListboxServiceContent::sort()
 {
-       ePtr<iListableService> lst;
-       if (!m_service_center->list(m_root, lst))
+       if (!m_lst)
+               m_service_center->list(m_root, m_lst);
+       if (m_lst)
        {
-               m_list.sort(iListableServiceCompare(lst));
+               m_list.sort(iListableServiceCompare(m_lst));
                        /* FIXME: is this really required or can we somehow keep the current entry? */
                cursorHome();
                if (m_listbox)
@@ -166,18 +260,31 @@ void eListboxServiceContent::sort()
 DEFINE_REF(eListboxServiceContent);
 
 eListboxServiceContent::eListboxServiceContent()
-       :m_epgcache(eEPGCache::getInstance()), m_visual_mode(visModeSimple), m_size(0), m_current_marked(false), m_numberoffset(0)
+       :m_visual_mode(visModeSimple), m_size(0), m_current_marked(false), m_numberoffset(0), m_itemheight(25)
 {
+       memset(m_color_set, 0, sizeof(m_color_set));
        cursorHome();
        eServiceCenter::getInstance(m_service_center);
-       if (eDVBResourceManager::getInstance(m_res_mgr))
-               eDebug("no resource manager");
+}
+
+void eListboxServiceContent::setColor(int color, gRGB &col)
+{
+       if ((color >= 0) && (color < colorElements))
+       {
+               m_color_set[color] = true;
+               m_color[color] = col;
+       }
 }
 
 void eListboxServiceContent::cursorHome()
 {
        if (m_current_marked && m_saved_cursor == m_list.end())
        {
+               if (m_cursor_number >= m_size)
+               {
+                       m_cursor_number = m_size-1;
+                       --m_cursor;
+               }
                while (m_cursor_number)
                {
                        std::iter_swap(m_cursor--, m_cursor);
@@ -226,13 +333,12 @@ int eListboxServiceContent::setCurrentMarked(bool state)
                m_listbox->entryChanged(m_cursor_number);
                if (!state)
                {
-                       ePtr<iListableService> lst;
-                       if (m_service_center->list(m_root, lst))
-                               eDebug("no list available!");
-                       else
+                       if (!m_lst)
+                               m_service_center->list(m_root, m_lst);
+                       if (m_lst)
                        {
                                ePtr<iMutableServiceList> list;
-                               if (lst->startEdit(list))
+                               if (m_lst->startEdit(list))
                                        eDebug("no editable list");
                                else
                                {
@@ -251,6 +357,8 @@ int eListboxServiceContent::setCurrentMarked(bool state)
                                        }
                                }
                        }
+                       else
+                               eDebug("no list available!");
                }
        }
 
@@ -330,125 +438,265 @@ int eListboxServiceContent::size()
 void eListboxServiceContent::setSize(const eSize &size)
 {
        m_itemsize = size;
-       setVisualMode(m_visual_mode);
+       if (m_visual_mode == visModeSimple)
+               setVisualMode(m_visual_mode);
 }
 
 void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
 {
        painter.clip(eRect(offset, m_itemsize));
 
-       bool tuneable=true;
+       int marked = 0;
+
+       if (m_current_marked && selected)
+               marked = 2;
+       else if (cursorValid() && isMarked(*m_cursor))
+       {
+               if (selected)
+                       marked = 2;
+               else
+                       marked = 1;
+       }
+       else
+               style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
+
+       eListboxStyle *local_style = 0;
+
+               /* get local listbox style, if present */
+       if (m_listbox)
+               local_style = m_listbox->getLocalStyle();
 
-       if (m_res_mgr && cursorValid() && !((m_cursor->flags & eServiceReference::flagDirectory) == eServiceReference::flagDirectory))
+       if (marked == 1)  // marked
+       {
+               style.setStyle(painter, eWindowStyle::styleListboxMarked);
+               if (m_color_set[markedForeground])
+                       painter.setForegroundColor(m_color[markedForeground]);
+               if (m_color_set[markedBackground])
+                       painter.setBackgroundColor(m_color[markedBackground]);
+       }
+       else if (marked == 2) // marked and selected
+       {
+               style.setStyle(painter, eWindowStyle::styleListboxMarkedAndSelected);
+               if (m_color_set[markedForegroundSelected])
+                       painter.setForegroundColor(m_color[markedForegroundSelected]);
+               if (m_color_set[markedBackgroundSelected])
+                       painter.setBackgroundColor(m_color[markedBackgroundSelected]);
+       }
+       else if (local_style)
        {
-               if ( eDVBServicePMTHandler::getCount() > 1 )
+               if (selected)
                {
-                       eServiceReferenceDVB &ref = (eServiceReferenceDVB&) *m_cursor;
-                       eUsePtr<iDVBChannel> channel;
-                       eDVBChannelID chid;
-                       ref.getChannelID(chid);
-                       tuneable = !m_res_mgr->allocateChannel(chid, channel, true);  // no real allocate channel..just fake
+                       /* if we have a local background color set, use that. */
+                       if (local_style->m_background_color_selected_set)
+                               painter.setBackgroundColor(local_style->m_background_color_selected);
+                       /* same for foreground */
+                       if (local_style->m_foreground_color_selected_set)
+                               painter.setForegroundColor(local_style->m_foreground_color_selected);
+               }
+               else
+               {
+                       /* if we have a local background color set, use that. */
+                       if (local_style->m_background_color_set)
+                               painter.setBackgroundColor(local_style->m_background_color);
+                       /* same for foreground */
+                       if (local_style->m_foreground_color_set)
+                               painter.setForegroundColor(local_style->m_foreground_color);
                }
        }
 
-       if (m_current_marked && selected)
-               style.setStyle(painter, eWindowStyle::styleListboxMarked);
-       else if (cursorValid() && isMarked(*m_cursor))
-               style.setStyle(painter, eWindowStyle::styleListboxMarked);
-       else
+       if (!local_style || !local_style->m_transparent_background)
+               /* if we have no transparent background */
        {
-               style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
-               if (!tuneable)
-                       painter.setForegroundColor(gRGB(0xbbbbbb));
+               /* blit background picture, if available (otherwise, clear only) */
+               if (local_style && local_style->m_background)
+                       painter.blit(local_style->m_background, offset, eRect(), 0);
+               else
+                       painter.clear();
+       } else
+       {
+               if (local_style->m_background)
+                       painter.blit(local_style->m_background, offset, eRect(), gPainter::BT_ALPHATEST);
+               else if (selected && !local_style->m_selection)
+                       painter.clear();
        }
-       painter.clear();
-       
+
        if (cursorValid())
        {
                        /* get service information */
                ePtr<iStaticServiceInformation> service_info;
                m_service_center->info(*m_cursor, service_info);
-               
-               for (int e = 0; e < celElements; ++e)
+               eServiceReference ref = *m_cursor;
+               bool isPlayable = !(ref.flags & eServiceReference::isDirectory || ref.flags & eServiceReference::isMarker);
+
+               if (!marked && isPlayable && service_info && m_is_playable_ignore.valid() && !service_info->isPlayable(*m_cursor, m_is_playable_ignore))
                {
-                       if (!m_element_font[e])
-                               continue;
-                       int flags=gPainter::RT_VALIGN_CENTER;
+                       if (m_color_set[serviceNotAvail])
+                               painter.setForegroundColor(m_color[serviceNotAvail]);
+                       else
+                               painter.setForegroundColor(gRGB(0xbbbbbb));
+               }
 
-                       eRect area = m_element_position[e];
+               if (selected && local_style && local_style->m_selection)
+                       painter.blit(local_style->m_selection, offset, eRect(), gPainter::BT_ALPHATEST);
 
-                       std::string text = "<n/a>";
+               int xoffset=0;  // used as offset when painting the folder/marker symbol
 
-                       switch (e)
-                       {
-                       case celIcon:
-                               // render icon here...
-                               continue;
-                       case celServiceNumber:
-                       {
-                               char bla[10];
-                               sprintf(bla, "%d", m_numberoffset + m_cursor_number + 1);
-                               text = bla;
-                               flags|=gPainter::RT_HALIGN_RIGHT;
-                               break;
-                       }
-                       case celServiceName:
-                       {
-                               if (service_info)
-                                       service_info->getName(*m_cursor, text);
-                               break;
-                       }
-                       case celServiceInfo:
+               for (int e = 0; e < celElements; ++e)
+               {
+                       if (m_element_font[e])
                        {
-                               ePtr<eServiceEvent> evt;
-                               time_t t=-1;
-                               if ( !m_epgcache->lookupEventTime(*m_cursor, t, evt) )
-                                       text = '(' + evt->getEventName() + ')';
-                               else
-                                       continue;
-                               break;
-                       }
-                       }
+                               int flags=gPainter::RT_VALIGN_CENTER,
+                                       yoffs = 0,
+                                       xoffs = xoffset;
+                               eRect &area = m_element_position[e];
+                               std::string text = "<n/a>";
+                               xoffset=0;
+
+                               switch (e)
+                               {
+                               case celServiceNumber:
+                               {
+                                       if (m_cursor->flags & eServiceReference::isMarker)
+                                               continue;
+                                       char bla[10];
+                               /* how we can do this better? :) */
+                                       int markers_before=0;
+                                       {
+                                               list::iterator tmp=m_cursor;
+                                               while(tmp != m_list.begin())
+                                               {
+                                                       --tmp;
+                                                       if (tmp->flags & eServiceReference::isMarker)
+                                                               ++markers_before;
+                                               }
+                                       }
+                                       sprintf(bla, "%d", m_numberoffset + m_cursor_number + 1 - markers_before);
+                                       text = bla;
+                                       flags|=gPainter::RT_HALIGN_RIGHT;
+                                       break;
+                               }
+                               case celServiceName:
+                               {
+                                       if (service_info)
+                                               service_info->getName(*m_cursor, text);
+                                       break;
+                               }
+                               case celServiceInfo:
+                               {
+                                       ePtr<eServiceEvent> evt;
+                                       if ( isPlayable && service_info && !service_info->getEvent(*m_cursor, evt) )
+                                       {
+                                               std::string name = evt->getEventName();
+                                               if (!name.length())
+                                                       continue;
+                                               text = '(' + evt->getEventName() + ')';
+                                       }
+                                       else
+                                               continue;
+                                       break;
+                               }
+                               }
 
-                       eTextPara *para = new eTextPara(area);
+                               eRect tmp = area;
+                               tmp.setWidth(tmp.width()-xoffs);
 
-                       para->setFont(m_element_font[e]);
-                       para->renderString(text);
+                               eTextPara *para = new eTextPara(tmp);
+                               para->setFont(m_element_font[e]);
+                               para->renderString(text.c_str());
 
-                       if (e == celServiceName)
-                       {
-                               eRect bbox = para->getBoundBox();
-                               int name_width = bbox.width()+10;
-                               m_element_position[celServiceInfo].setLeft(area.left()+name_width);
-                               m_element_position[celServiceInfo].setTop(area.top());
-                               m_element_position[celServiceInfo].setWidth(area.width()-name_width);
-                               m_element_position[celServiceInfo].setHeight(area.height());
-                       }
+                               if (e == celServiceName)
+                               {
+                                       eRect bbox = para->getBoundBox();
+                                       int name_width = bbox.width()+8;
+                                       m_element_position[celServiceInfo].setLeft(area.left()+name_width);
+                                       m_element_position[celServiceInfo].setTop(area.top());
+                                       m_element_position[celServiceInfo].setWidth(area.width()-name_width);
+                                       m_element_position[celServiceInfo].setHeight(area.height());
+                               }
 
-                       if (flags & gPainter::RT_HALIGN_RIGHT)
-                               para->realign(eTextPara::dirRight);
-                       else if (flags & gPainter::RT_HALIGN_CENTER)
-                               para->realign(eTextPara::dirCenter);
-                       else if (flags & gPainter::RT_HALIGN_BLOCK)
-                               para->realign(eTextPara::dirBlock);
+                               if (flags & gPainter::RT_HALIGN_RIGHT)
+                                       para->realign(eTextPara::dirRight);
+                               else if (flags & gPainter::RT_HALIGN_CENTER)
+                                       para->realign(eTextPara::dirCenter);
+                               else if (flags & gPainter::RT_HALIGN_BLOCK)
+                                       para->realign(eTextPara::dirBlock);
 
-                       ePoint offs = offset;
+                               if (flags & gPainter::RT_VALIGN_CENTER)
+                               {
+                                       eRect bbox = para->getBoundBox();
+                                       int vcentered_top = (area.height() - bbox.height()) / 2;
+                                       yoffs = vcentered_top - bbox.top();
+                               }
 
-                       if (flags & gPainter::RT_VALIGN_CENTER)
-                       {
-                               eRect bbox = para->getBoundBox();
-                               int vcentered_top = (area.height() - bbox.height()) / 2;
-                               int correction = vcentered_top - bbox.top();
-                               offs += ePoint(0, correction);
+                               painter.renderPara(para, offset+ePoint(xoffs, yoffs));
                        }
+                       else if (e == celServiceTypePixmap || e == celFolderPixmap || e == celMarkerPixmap)
+                       {
+                               int orbpos = m_cursor->getUnsignedData(4) >> 16;
+                               ePtr<gPixmap> &pixmap =
+                                       (e == celFolderPixmap) ? m_pixmaps[picFolder] :
+                                       (e == celMarkerPixmap) ? m_pixmaps[picMarker] :
+                                       (m_cursor->flags & eServiceReference::isGroup) ? m_pixmaps[picServiceGroup] :
+                                       (orbpos == 0xFFFF) ? m_pixmaps[picDVB_C] :
+                                       (orbpos == 0xEEEE) ? m_pixmaps[picDVB_T] : m_pixmaps[picDVB_S];
+                               if (pixmap)
+                               {
+                                       eSize pixmap_size = pixmap->size();
+                                       int p = celServiceInfo;
+                                       if (e == celFolderPixmap)
+                                               p = celServiceName;
+                                       else if (e == celMarkerPixmap)
+                                               p = celServiceNumber;
+                                       eRect area = m_element_position[p];
+                                       int correction = (area.height() - pixmap_size.height()) / 2;
+
+                                       if (isPlayable)
+                                       {
+                                               if (e != celServiceTypePixmap)
+                                                       continue;
+                                               m_element_position[celServiceInfo] = area;
+                                               m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + 8);
+                                               m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - 8);
+                                       }
+                                       else if (m_cursor->flags & eServiceReference::isDirectory)
+                                       {
+                                               if (e != celFolderPixmap)
+                                                       continue;
+                                               xoffset = pixmap_size.width() + 8;
+                                       }
+                                       else if (m_cursor->flags & eServiceReference::isMarker)
+                                       {
+                                               if (e != celMarkerPixmap)
+                                                       continue;
+                                       }
+                                       else
+                                               eFatal("unknown service type in listboxservice");
 
-                       painter.renderPara(para, offs);
+                                       area.moveBy(offset);
+                                       painter.clip(area);
+                                       painter.blit(pixmap, offset+ePoint(area.left(), correction), area, gPainter::BT_ALPHATEST);
+                                       painter.clippop();
+                               }
+                       }
                }
                
-               if (selected)
+               if (selected && (!local_style || !local_style->m_selection))
                        style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
        }
        
        painter.clippop();
 }
 
+void eListboxServiceContent::setIgnoreService( const eServiceReference &service )
+{
+       m_is_playable_ignore=service;
+       if (m_listbox && m_listbox->isVisible())
+               m_listbox->invalidate();
+}
+
+void eListboxServiceContent::setItemHeight(int height)
+{
+       m_itemheight = height;
+       if (m_listbox)
+               m_listbox->setItemHeight(height);
+}