Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

mediainfocombobox.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 - 2008 by Martin Mueller                           *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "mediainfocombobox.h"
00021 #include "mediainfocombobox.moc"
00022 
00023 /*#include <dcopclient.h>
00024 #include <dcopref.h>
00025 #include <kapplication.h>
00026 */
00027 #include <kwindowsystem.h>
00028 #include <kwindowinfo.h>
00029 #include <kicon.h>
00030 #include <kdebug.h>
00031 
00032 class Blokkal::Ui::MediaInfoComboBox::Private
00033 {
00034 public:
00035         Private( void ) {}
00036 };
00037 
00038 Blokkal::Ui::MediaInfoComboBox::MediaInfoComboBox( QWidget * parent ) :
00039 SemiEditableComboBox( parent ),
00040 d( new Private() )
00041 {
00042         setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
00043         repopulate();
00044         connect( this, SIGNAL( activated( const QString & ) ),
00045                  this, SLOT( repopulate( void ) ) );
00046 }
00047 
00048 Blokkal::Ui::MediaInfoComboBox::~MediaInfoComboBox( void )
00049 {
00050         delete d;
00051 }
00052 
00053 void Blokkal::Ui::MediaInfoComboBox::repopulate( void )
00054 {
00055         const QList<WId> windowList = KWindowSystem::windows();
00056         QString windowName;
00057         bool boxWasEmpty = FALSE;
00058         int oldCount = count();
00059         int currentListItem = 0;
00060         
00061         //first item is for manual editing
00062         //make sure it exists
00063         if( count() <= currentListItem ) {
00064                 addItem( QString::null, currentListItem );
00065                 boxWasEmpty = TRUE;
00066         }
00067         currentListItem++;
00068 
00069         //find XMMS window
00070         //then follow xmms windows
00071         for ( QList<WId>::ConstIterator winListIterator = windowList.begin();
00072               winListIterator != windowList.end() && !windowList.isEmpty();
00073               ++winListIterator )
00074         {
00075                 if( !KWindowSystem::hasWId( *winListIterator ) ) {
00076                         continue;
00077                 }
00078                 const KWindowInfo windowInfo = KWindowSystem::windowInfo( *winListIterator, NET::WMVisibleName|NET::WMIconName, NET::WM2StartupId );
00079                 if( windowInfo.visibleName().indexOf( "XMMS - " ) == 0) {
00080                         int musicInfoStart = windowInfo.visibleName().indexOf( " ", 7 ) + 1;
00081                         int musicInfoLength = windowInfo.visibleName().lastIndexOf( " " ) - musicInfoStart;
00082                         if( count() <= currentListItem ) {
00083                                 insertItem( currentListItem,
00084                                             KWindowSystem::icon( *winListIterator,
00085                                                                  fontMetrics().height(),
00086                                                                  fontMetrics().height(),
00087                                                                  TRUE ),
00088                                             windowInfo.visibleName().mid( musicInfoStart, musicInfoLength ) );
00089                         }
00090                         else {
00091                                 setItemText( currentListItem,
00092                                              windowInfo.visibleName().mid( musicInfoStart, musicInfoLength ) );
00093                         }
00094                         currentListItem++;
00095                 }
00096         }
00097         //done with the windows
00098 
00099         /*
00100         //Check amaroK
00101         DCOPReply reply = DCOPRef( "amarok", "player" ).call( "nowPlaying" );
00102         if( reply.isValid() ) {
00103                 QString result = "Mother Russia";//reply;
00104                 if( count() <= currentListItem ) {
00105                         addItem( result, currentListItem );
00106                 }
00107                 else {
00108                         setItemText( currentListItem, "Mother Russia" );
00109                 }
00110                 currentListItem++;
00111         }*/
00112 
00113         
00114         //select the last player by default, should be just one most of the time
00115         if( boxWasEmpty || oldCount < count() ) {
00116                 setCurrentIndex( count()-1 );
00117         }
00118 
00119         updateEditableState();
00120 }