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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2012-02-12
0007  * Description : a tool to export images to IPFS web service
0008  *
0009  * SPDX-FileCopyrightText: 2018      by Amar Lakshya <amar dot lakshya at xaviers dot edu dot in>
0010  * SPDX-FileCopyrightText: 2018-2020 by Caulier Gilles <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "ipfsimageslist.h"
0017 
0018 // Qt includes
0019 
0020 #include <QLabel>
0021 #include <QPointer>
0022 #include <QDesktopServices>
0023 
0024 // KDE includes
0025 
0026 #include <klocalizedstring.h>
0027 
0028 // Local includes
0029 
0030 #include "digikam_debug.h"
0031 #include "dmetadata.h"
0032 
0033 namespace DigikamGenericIpfsPlugin
0034 {
0035 
0036 IpfsImagesList::IpfsImagesList(QWidget* const parent)
0037     : DItemsList(parent)
0038 {
0039     setControlButtonsPlacement(DItemsList::ControlButtonsBelow);
0040     setAllowDuplicate(false);
0041     setAllowRAW(false);
0042 
0043     auto* list = listView();
0044 
0045     list->setColumnLabel(DItemsListView::Thumbnail, i18n("Thumbnail"));
0046 
0047     list->setColumnLabel(static_cast<DItemsListView::ColumnType>(IpfsImagesList::Title),
0048                          i18n("Submission title"));
0049 
0050     list->setColumnLabel(static_cast<DItemsListView::ColumnType>(IpfsImagesList::Description),
0051                          i18n("Submission description"));
0052 
0053     list->setColumn(static_cast<DItemsListView::ColumnType>(IpfsImagesList::Url),
0054                     i18n("Ipfs Url"), true);
0055 
0056     connect(list, &DItemsListView::itemDoubleClicked,
0057             this, &IpfsImagesList::slotDoubleClick);
0058 }
0059 
0060 QList<const IpfsImagesListViewItem*> IpfsImagesList::getPendingItems() const
0061 {
0062     QList<const IpfsImagesListViewItem*> ret;
0063 
0064     for (int i = 0 ; i < listView()->topLevelItemCount() ; ++i)
0065     {
0066         auto* const item = dynamic_cast<const IpfsImagesListViewItem*>(listView()->topLevelItem(i));
0067 
0068         if (item && item->IpfsUrl().isEmpty())
0069         {
0070             ret << item;
0071         }
0072     }
0073 
0074     return ret;
0075 }
0076 
0077 void IpfsImagesList::slotAddImages(const QList<QUrl>& list)
0078 {
0079     /**
0080      * NOTE: Replaces the DItemsList::slotAddImages method, so that
0081      * IpfsImagesListViewItems can be added instead of ImagesListViewItems
0082      */
0083 
0084     QScopedPointer<DMetadata> meta(new DMetadata);
0085 
0086     for (QList<QUrl>::ConstIterator it = list.constBegin() ; it != list.constEnd() ; ++it)
0087     {
0088         // Already in the list?
0089 
0090         if (listView()->findItem(*it) == nullptr)
0091         {
0092             // Load URLs from meta data, if possible
0093 
0094             if (meta->load((*it).toLocalFile()))
0095             {
0096                 auto* const item = new IpfsImagesListViewItem(listView(), *it);
0097                 item->setIpfsUrl(meta->getXmpTagString("Xmp.digiKam.IPFSId"));
0098             }
0099         }
0100     }
0101 
0102     Q_EMIT signalImageListChanged();
0103     Q_EMIT signalAddItems(list);
0104 }
0105 
0106 void IpfsImagesList::slotSuccess(const IpfsTalkerResult& result)
0107 {
0108     QUrl ipfsl = QUrl::fromLocalFile(result.action->upload.imgpath);
0109 
0110     processed(ipfsl, true);
0111 
0112     QScopedPointer<DMetadata> meta(new DMetadata);
0113 
0114     // Save URLs to meta data, if possible
0115 
0116     if (meta->load(ipfsl.toLocalFile()))
0117     {
0118         meta->setXmpTagString("Xmp.digiKam.IPFSId", result.image.url);
0119         bool saved = meta->applyChanges();
0120         qCDebug(DIGIKAM_WEBSERVICES_LOG) << "Metadata" << (saved ? "Saved" : "Not Saved") << "to" << ipfsl;
0121     }
0122 
0123     IpfsImagesListViewItem* const currItem = dynamic_cast<IpfsImagesListViewItem*>(listView()->findItem(ipfsl));
0124 
0125     if (!currItem)
0126         return;
0127 
0128     if (!result.image.url.isEmpty())
0129         currItem->setIpfsUrl(result.image.url);
0130 }
0131 
0132 void IpfsImagesList::slotDoubleClick(QTreeWidgetItem* element, int i)
0133 {
0134     if (i == Url)
0135     {
0136         const QUrl url = QUrl(element->text(i));
0137 
0138         // The delete page asks for confirmation, so we don't need to do that here
0139 
0140         QDesktopServices::openUrl(url);
0141     }
0142 }
0143 
0144 // ------------------------------------------------------------------------------------------------
0145 
0146 IpfsImagesListViewItem::IpfsImagesListViewItem(DItemsListView* const view, const QUrl& url)
0147     : DItemsListViewItem(view, url)
0148 {
0149     QBrush blue(QColor(50, 50, 255));
0150 
0151     setForeground(IpfsImagesList::Url, blue);
0152 }
0153 
0154 void IpfsImagesListViewItem::setTitle(const QString& str)
0155 {
0156     setText(IpfsImagesList::Title, str);
0157 }
0158 
0159 QString IpfsImagesListViewItem::Title() const
0160 {
0161     return text(IpfsImagesList::Title);
0162 }
0163 
0164 void IpfsImagesListViewItem::setDescription(const QString& str)
0165 {
0166     setText(IpfsImagesList::Description, str);
0167 }
0168 
0169 QString IpfsImagesListViewItem::Description() const
0170 {
0171     return text(IpfsImagesList::Description);
0172 }
0173 
0174 void IpfsImagesListViewItem::setIpfsUrl(const QString& str)
0175 {
0176     setText(IpfsImagesList::Url, str);
0177 }
0178 
0179 QString IpfsImagesListViewItem::IpfsUrl() const
0180 {
0181     return text(IpfsImagesList::Url);
0182 }
0183 
0184 } // namespace DigikamGenericIpfsPlugin
0185 
0186 #include "moc_ipfsimageslist.cpp"