File indexing completed on 2025-01-05 03:53:06

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-07-24
0007  * Description : MJPEG Stream configuration dialog - Server methods
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2021      by Quoc Hưng Tran <quochungtran1999 at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "mjpegstreamdlg_p.h"
0017 
0018 namespace DigikamGenericMjpegStreamPlugin
0019 {
0020 
0021 void MjpegStreamDlg::updateServerStatus()
0022 {
0023     if (d->mngr->isRunning())
0024     {
0025         d->srvStatus->setText(i18nc("@label", "Server is running"));
0026         d->aStats->setText(i18ncp("@info", "1 album shared", "%1 albums shared", d->mngr->albumsShared()));
0027         d->separator->setVisible(true);
0028         d->iStats->setText(i18ncp("@info", "1 item shared",  "%1 items shared",  d->mngr->itemsShared()));
0029         d->srvButton->setText(i18nc("@action: button", "Stop"));
0030         d->srvButton->setIcon(QIcon::fromTheme(QLatin1String("media-playback-stop")));
0031         d->progress->toggleTimer(true);
0032         d->progress->setVisible(true);
0033         d->srvPreview->setVisible(true);
0034     }
0035     else
0036     {
0037         d->srvStatus->setText(i18nc("@label", "Server is not running"));
0038         d->aStats->clear();
0039         d->separator->setVisible(false);
0040         d->iStats->clear();
0041         d->srvButton->setText(i18nc("@action: button", "Start"));
0042         d->srvButton->setIcon(QIcon::fromTheme(QLatin1String("media-playback-start")));
0043         d->progress->toggleTimer(false);
0044         d->progress->setVisible(false);
0045         d->srvPreview->setVisible(false);
0046     }
0047 }
0048 
0049 bool MjpegStreamDlg::setMjpegServerContents()
0050 {
0051     if (d->albumSupport)
0052     {
0053         DInfoInterface::DAlbumIDs albums = d->settings.iface->albumChooserItems();
0054         MjpegServerMap map;
0055 
0056         Q_FOREACH (int id, albums)
0057         {
0058             DAlbumInfo anf(d->settings.iface->albumInfo(id));
0059             map.insert(anf.title(), d->settings.iface->albumItems(id));
0060         }
0061 
0062         if (map.isEmpty())
0063         {
0064             QMessageBox::information(this, i18nc("@title:window", "Media Server Contents"),
0065                                      i18nc("@info", "There is no collection to share with the current selection..."));
0066             return false;
0067         }
0068 
0069         d->mngr->setCollectionMap(map);
0070     }
0071     else
0072     {
0073         QList<QUrl> urls = d->listView->imageUrls();
0074 
0075         if (urls.isEmpty())
0076         {
0077             QMessageBox::information(this, i18nc("@title:window", "Media Server Contents"),
0078                                      i18nc("@info", "There is no item to share with the current selection..."));
0079 
0080             return false;
0081         }
0082 
0083         d->mngr->setItemsList(i18nc("@info", "Shared Items"), urls);
0084     }
0085 
0086     return true;
0087 }
0088 
0089 bool MjpegStreamDlg::startMjpegServer()
0090 {
0091     if (d->dirty)
0092     {
0093         d->dirty = false;
0094     }
0095 
0096     if (!setMjpegServerContents())
0097     {
0098         return false;
0099     }
0100 
0101     d->mngr->setSettings(d->settings);
0102 
0103     if (!d->mngr->startMjpegServer())
0104     {
0105         QMessageBox::warning(this, i18nc("@title:window", "Starting Media Server"),
0106                              i18nc("@info", "An error occurs while to start Media Server..."));
0107     }
0108     else
0109     {
0110         d->mngr->mjpegServerNotification(true);
0111     }
0112 
0113     updateServerStatus();
0114 
0115     return true;
0116 }
0117 
0118 void MjpegStreamDlg::slotSelectionChanged()
0119 {
0120     d->dirty = true;
0121 }
0122 
0123 void MjpegStreamDlg::slotToggleMjpegServer()
0124 {
0125     bool b = false;
0126 
0127     if (!d->mngr->isRunning())
0128     {
0129         if (startMjpegServer())
0130         {
0131             b = true;
0132         }
0133     }
0134     else
0135     {
0136         d->mngr->cleanUp();
0137         updateServerStatus();
0138     }
0139 
0140     d->tabView->setTabEnabled(Private::Stream,     !b);
0141     d->tabView->setTabEnabled(Private::Effect,     !b);
0142     d->tabView->setTabEnabled(Private::Transition, !b);
0143     d->tabView->setTabEnabled(Private::OSD,        !b);
0144 
0145     d->srvPort->setDisabled(b);
0146 }
0147 
0148 } // namespace DigikamGenericMjpegStreamPlugin