settings_stream_widget_connection.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2008-2010  Lukas Sommer < SommerLuk at gmail dot com >
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU General Public License as
00006     published by the Free Software Foundation; either version 2 of
00007     the License or (at your option) version 3 or any later version
00008     accepted by the membership of KDE e.V. (or its successor approved
00009     by the membership of KDE e.V.), which shall act as a proxy
00010     defined in Section 14 of version 3 of the license.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "settings_stream_widget_connection.h"
00022 #define AND  &&
00023 #define OR  ||
00024 #define NOT  !
00025 #define EQUAL  ==
00026 
00027 settings_stream_widget_connection::settings_stream_widget_connection(QWidget *parent)
00028   : QStackedWidget(parent)
00029 {
00030   setupUi(this);
00031 
00032   // Set the history list. Doing this in the .ui file whould result in marking these
00033   // strings for translation - and that would be bad.
00034   QStringList predefinedUserAgentStrings;
00035   predefinedUserAgentStrings.append(QString("KStreamRipper"));
00036   predefinedUserAgentStrings.append(QString("Streamripper"));
00037   predefinedUserAgentStrings.append(QString("WinampMPEG/5.0"));
00038   predefinedUserAgentStrings.append(QString("Winamp/2.x"));
00039   predefinedUserAgentStrings.append(QString("FreeAmp/2.x"));
00040   predefinedUserAgentStrings.append(QString("XMMS/1.x"));
00041   predefinedUserAgentStrings.append(QString("UnknownPlayer/1.x"));
00042   predefinedUserAgentStrings.append(QString("IE 5.0"));
00043   predefinedUserAgentStrings.append(QString("iTunes/4.7 (Macintosh; N; PPC))"));
00044   predefinedUserAgentStrings.append(QString("RMA/1.0 (compatible; RealMedia))"));
00045   kcfg_advanced_userAgentString->setHistoryItems(predefinedUserAgentStrings);
00046 }
00047 
00048 // TODO nach 15 sec oder welcher Zeit auch immer sollte die Erkennung abgebrochen werden... Timeout!
00049 
00050 settings_stream_widget_connection::~settings_stream_widget_connection()
00051 {
00052   helper_disconnect_m_process_and_kill();  // make sure that m_process
00053   // stops emitting signals while we are yet destroing everything her.
00054   // I don't know why, but when we don't do this, the application
00055   // crashes with "signal 6/SIGABORT: pure virtual method called".
00056 }
00057 
00058 void settings_stream_widget_connection::helper_setServerUri_connectMProcess_startRecognization()
00059 {
00060   m_process.setServerUri(kcfg_serverUri->text());
00061   connect(
00062     &m_process,
00063     SIGNAL(not_running()),
00064     this,
00065     SLOT(change_empty_user_visible_widgets_to__connection_failed()));
00066   connect(&m_process,
00067            SIGNAL(streamNameChanged(void *, PropertyValue)),
00068            this,
00069            SLOT(setStreamName(void *, PropertyValue)));
00070   connect(&m_process,
00071            SIGNAL(serverNameChanged(void *, PropertyValue)),
00072            this,
00073            SLOT(setServerName(void *, PropertyValue)));
00074   connect(&m_process,
00075            SIGNAL(bitrateChanged(void *, PropertyValue)),
00076            this,
00077            SLOT(setBitrate(void *, PropertyValue)));
00078   connect(&m_process,
00079            SIGNAL(metaIntervalChanged(void *, PropertyValue)),
00080            this,
00081            SLOT(setMetaInterval(void *, PropertyValue)));
00082   m_process.startStreamripper();
00083 }
00084 
00085 void settings_stream_widget_connection::helper_disconnect_m_process_and_kill()
00086 {
00087   disconnect(
00088     &m_process,
00089     SIGNAL(not_running()),
00090     this,
00091     SLOT(change_empty_user_visible_widgets_to__connection_failed()));
00092   disconnect(&m_process,
00093               SIGNAL(streamNameChanged(void *, PropertyValue)),
00094               this,
00095               SLOT(setStreamName(void *, PropertyValue)));
00096   disconnect(&m_process,
00097               SIGNAL(serverNameChanged(void *, PropertyValue)),
00098               this,
00099               SLOT(setServerName(void *, PropertyValue)));
00100   disconnect(&m_process,
00101               SIGNAL(bitrateChanged(void *, PropertyValue)),
00102               this,
00103               SLOT(setBitrate(void *, PropertyValue)));
00104   disconnect(&m_process,
00105               SIGNAL(metaIntervalChanged(void *, PropertyValue)),
00106               this,
00107               SLOT(setMetaInterval(void *, PropertyValue)));
00108   m_process.abortStreamripper();
00109 }
00110 
00111 QString settings_stream_widget_connection::helper_qstring_localized__recognizing()
00112 {
00113   return i18nc("@item This is displayed in the stream settings dialog for the stream info after "
00114                  "entering a new URI",
00115                "Recognizing...");
00116 }
00117 
00118 void settings_stream_widget_connection::delete_old_streamInfo_and_start_recognization()
00119 {
00120   helper_disconnect_m_process_and_kill();
00121 
00122   // delete the values in the config system and update the GUI
00123   kcfg_info_serverName->setText(ripping::default_value_of_serverName());
00124   kcfg_info_streamName->setText(ripping::default_value_of_streamName());
00125   kcfg_info_bitrate->setValue(ripping::default_value_of_bitrate());
00126   kcfg_info_metaInterval->setValue(ripping::default_value_of_metaInterval());
00127 
00128   info_serverName->setText(helper_qstring_localized__recognizing());
00129   info_streamName->setText(helper_qstring_localized__recognizing());
00130   info_bitrate->setText(helper_qstring_localized__recognizing());
00131   info_metaInterval->setText(helper_qstring_localized__recognizing());
00132   info_serverName->setEnabled(false);
00133   info_streamName->setEnabled(false);
00134   info_bitrate->setEnabled(false);
00135   info_metaInterval->setEnabled(false);
00136 
00137   helper_setServerUri_connectMProcess_startRecognization();
00138 }
00139 
00140 // TODO what does this class, if the streamripper-binary isn't found?
00141 
00142 void settings_stream_widget_connection::load_info_from_kcfg_and_start_recognization()
00143 {
00144   connect(kcfg_serverUri, SIGNAL(textChanged(const QString&)), this,
00145            SLOT(delete_old_streamInfo_and_start_recognization()));
00146 
00147   /* This "connect" must be done here and can not be done in the constructor.
00148 
00149      Imagine that it would be done in the constructor. The widget is constructed
00150      and the signal is connected. Than, the widget is added to a KConfigDialog;
00151      and this KConfigDialog will set the text field kcfg_serverUri to a value
00152      taken from a config file. And than, delete_old_streamInfo_and_start_recognization()
00153      would be called. But we don't want the old info be deleted - when there's no
00154      Internet connection at this moment, the fields would be deleted :-(And exactly
00155      that do we want to prevent.
00156   */
00157 
00158   helper_disconnect_m_process_and_kill();  // just to be sure...
00159 
00160   PropertyValue temp_serverName = ripping::formatedServerName(kcfg_info_serverName->text());
00161   if (temp_serverName.type != PropertyValue::value) {
00162     temp_serverName.formatedValue = helper_qstring_localized__recognizing();
00163   };
00164   setServerName(0, temp_serverName);
00165 
00166   PropertyValue temp_streamName = ripping::formatedStreamName(kcfg_info_streamName->text());
00167   if (temp_streamName.type != PropertyValue::value) {
00168     temp_streamName.formatedValue = helper_qstring_localized__recognizing();
00169   };
00170   setStreamName(0, temp_streamName);
00171 
00172   PropertyValue temp_bitrate = ripping::formatedBitrate(kcfg_info_bitrate->value());
00173   if (temp_bitrate.type != PropertyValue::value) {
00174     temp_bitrate.formatedValue = helper_qstring_localized__recognizing();
00175   };
00176   setBitrate(0, temp_bitrate);
00177 
00178   PropertyValue temp_metaInterval =
00179     ripping::formatedMetaInterval(kcfg_info_metaInterval->value());
00180   if (temp_metaInterval.type != PropertyValue::value) {
00181     temp_metaInterval.formatedValue = helper_qstring_localized__recognizing();
00182   };
00183   setMetaInterval(0, temp_metaInterval);
00184 
00185   helper_setServerUri_connectMProcess_startRecognization();
00186 }
00187 
00188 void settings_stream_widget_connection::setStreamName(void *,
00189                                                       const PropertyValue & streamName)
00190 {
00191   kcfg_info_streamName->setText(streamName.internalValue.toString());
00192   info_streamName->setText(streamName.formatedValue);
00193   if (streamName.type == PropertyValue::value) {
00194     info_streamName->setEnabled(true);
00195   } else {
00196     info_streamName->setEnabled(false);
00197   };
00198 }
00199 
00200 void settings_stream_widget_connection::setServerName(void *,
00201                                                       const PropertyValue & serverName)
00202 {
00203   kcfg_info_serverName->setText(serverName.internalValue.toString());
00204   info_serverName->setText(serverName.formatedValue);
00205   if (serverName.type == PropertyValue::value) {
00206     info_serverName->setEnabled(true);
00207   } else {
00208     info_serverName->setEnabled(false);
00209   };
00210 }
00211 
00212 void settings_stream_widget_connection::setBitrate(void *,
00213                                                    const PropertyValue & bitrate)
00214 {
00215   kcfg_info_bitrate->setValue(bitrate.internalValue.toLongLong());
00216   info_bitrate->setText(bitrate.formatedValue);
00217   if (bitrate.type == PropertyValue::value) {
00218     info_bitrate->setEnabled(true);
00219   } else {
00220     info_bitrate->setEnabled(false);
00221   };
00222 }
00223 
00224 void settings_stream_widget_connection::setMetaInterval(void *,
00225                                                         const PropertyValue & metaInterval)
00226 {
00227   kcfg_info_metaInterval->setValue(metaInterval.internalValue.toLongLong());
00228   info_metaInterval->setText(metaInterval.formatedValue);
00229   if (metaInterval.type == PropertyValue::value) {
00230     info_metaInterval->setEnabled(true);
00231   } else {
00232     info_metaInterval->setEnabled(false);
00233   };
00234 }
00235 
00236 void settings_stream_widget_connection::change_empty_user_visible_widgets_to__connection_failed()
00237 {
00238   // if values hasn't been recognized (that's different from "error during recognization"!)
00239   if (ripping::formatedServerName(kcfg_info_serverName->text()).type == PropertyValue::unset) {
00240     info_serverName->setText(i18nc("@item This is displayed in the stream settings dialog for "
00241                                        "the stream info after entering a new URI",
00242                                    "Connection failed."));
00243   };
00244   if (ripping::formatedServerName(kcfg_info_streamName->text()).type == PropertyValue::unset) {
00245     info_streamName->setText(i18nc("@item This is displayed in the stream settings dialog for "
00246                                        "the stream info after entering a new URI",
00247                                    "Connection failed."));
00248   };
00249   if (ripping::formatedBitrate(kcfg_info_bitrate->value()).type == PropertyValue::unset) {
00250     info_bitrate->setText(i18nc("@item This is displayed in the stream settings dialog for "
00251                                        "the stream info after entering a new URI",
00252                                 "Connection failed."));
00253   };
00254   if (ripping::formatedMetaInterval(kcfg_info_metaInterval->value()).type ==
00255        PropertyValue::unset) {
00256     info_metaInterval->setText(i18nc("@item This is displayed in the stream settings dialog for "
00257                                        "the stream info after entering a new URI",
00258                                      "Connection failed."));
00259   };
00260 }

doxygen