File indexing completed on 2025-01-19 03:52:58

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2005-07-07
0007  * Description : a tool to export images to Flickr web service
0008  *
0009  * SPDX-FileCopyrightText: 2005-2008 by Vardhman Jain <vardhman at gmail dot com>
0010  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2009      by Luka Renko <lure at kubuntu dot org>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "flickrwidget_p.h"
0018 
0019 namespace DigikamGenericFlickrPlugin
0020 {
0021 
0022 FlickrWidget::FlickrWidget(QWidget* const parent,
0023                            DInfoInterface* const iface,
0024                            const QString& serviceName)
0025     : WSSettingsWidget(parent, iface, serviceName),
0026       d               (new Private)
0027 {
0028     d->serviceName         = serviceName;
0029 
0030     // Adding Remove Account button
0031 
0032     d->removeAccount       = new QPushButton(getAccountBox());
0033     d->removeAccount->setText(i18n("Remove Account"));
0034     getAccountBoxLayout()->addWidget(d->removeAccount, 2, 0, 1, 4);
0035 
0036     // -- The image list --------------------------------------------------
0037 
0038     d->imglst              = new FlickrList(this);
0039 
0040     // For figuring out the width of the permission columns.
0041 
0042     QHeaderView* const hdr = d->imglst->listView()->header();
0043     QFontMetrics hdrFont   = QFontMetrics(hdr->font());
0044     int permColWidth       = hdrFont.horizontalAdvance(i18nc("photo permissions", "Public"));
0045 
0046     d->imglst->setAllowRAW(true);
0047     d->imglst->setIface(iface);
0048     d->imglst->loadImagesFromCurrentSelection();
0049 
0050     d->imglst->listView()->setWhatsThis(i18n("This is the list of images to upload to your Flickr account."));
0051     d->imglst->listView()->setColumn(static_cast<DItemsListView::ColumnType>(FlickrList::PUBLIC), i18nc("photo permissions", "Public"), true);
0052 
0053     // Handle extra tags per image.
0054 
0055     d->imglst->listView()->setColumn(static_cast<DItemsListView::ColumnType>(FlickrList::TAGS),
0056                                     i18n("Extra tags"), true);
0057 
0058     int tmpWidth;
0059 
0060     if ((tmpWidth = hdrFont.horizontalAdvance(i18nc("photo permissions", "Family"))) > permColWidth)
0061     {
0062         permColWidth = tmpWidth;
0063     }
0064 
0065     if ((tmpWidth = hdrFont.horizontalAdvance(i18nc("photo permissions", "Friends"))) > permColWidth)
0066     {
0067         permColWidth = tmpWidth;
0068     }
0069 
0070     d->imglst->listView()->setColumn(static_cast<DItemsListView::ColumnType>(FlickrList::FAMILY),
0071                                      i18nc("photo permissions", "Family"), true);
0072     d->imglst->listView()->setColumn(static_cast<DItemsListView::ColumnType>(FlickrList::FRIENDS),
0073                                      i18nc("photo permissions", "Friends"), true);
0074     hdr->setSectionResizeMode(FlickrList::FAMILY,  QHeaderView::Interactive);
0075     hdr->setSectionResizeMode(FlickrList::FRIENDS, QHeaderView::Interactive);
0076     hdr->resizeSection(FlickrList::FAMILY,  permColWidth);
0077     hdr->resizeSection(FlickrList::FRIENDS, permColWidth);
0078 
0079     d->imglst->listView()->setColumn(static_cast<DItemsListView::ColumnType>(FlickrList::SAFETYLEVEL),
0080                                      i18n("Safety level"), true);
0081     d->imglst->listView()->setColumn(static_cast<DItemsListView::ColumnType>(FlickrList::CONTENTTYPE),
0082                                      i18n("Content type"), true);
0083     QMap<int, QString> safetyLevelItems;
0084     QMap<int, QString> contentTypeItems;
0085     safetyLevelItems[FlickrList::SAFE]       = i18nc("photo safety level", "Safe");
0086     safetyLevelItems[FlickrList::MODERATE]   = i18nc("photo safety level", "Moderate");
0087     safetyLevelItems[FlickrList::RESTRICTED] = i18nc("photo safety level", "Restricted");
0088     contentTypeItems[FlickrList::PHOTO]      = i18nc("photo content type", "Photo");
0089     contentTypeItems[FlickrList::SCREENSHOT] = i18nc("photo content type", "Screenshot");
0090     contentTypeItems[FlickrList::OTHER]      = i18nc("photo content type", "Other");
0091     ComboBoxDelegate* const safetyLevelDelegate = new ComboBoxDelegate(d->imglst, safetyLevelItems);
0092     ComboBoxDelegate* const contentTypeDelegate = new ComboBoxDelegate(d->imglst, contentTypeItems);
0093     d->imglst->listView()->setItemDelegateForColumn(static_cast<DItemsListView::ColumnType>(FlickrList::SAFETYLEVEL), safetyLevelDelegate);
0094     d->imglst->listView()->setItemDelegateForColumn(static_cast<DItemsListView::ColumnType>(FlickrList::CONTENTTYPE), contentTypeDelegate);
0095 
0096     hdr->setSectionResizeMode(FlickrList::PUBLIC, QHeaderView::Interactive);
0097     hdr->resizeSection(FlickrList::PUBLIC, permColWidth);
0098 
0099     // -- Show upload original image CheckBox----------------------------------
0100 
0101     getOriginalCheckBox()->show();
0102 
0103     // -- Layout for the tags -------------------------------------------------
0104 
0105     QGroupBox* const tagsBox          = new QGroupBox(i18n("Tag options"), getSettingsBox());
0106     QGridLayout* const tagsBoxLayout  = new QGridLayout(tagsBox);
0107 
0108     d->exportHostTagsCheckBox         = new QCheckBox(tagsBox);
0109     d->exportHostTagsCheckBox->setText(i18n("Use Host Application Tags"));
0110 
0111     d->extendedTagsButton             = new QPushButton(i18n("More tag options"));
0112     d->extendedTagsButton->setCheckable(true);
0113 
0114     // Initialize this button to checked, so extended options are shown.
0115     // FlickrWindow::readSettings can change this, but if checked is false it
0116     // cannot uncheck and subsequently hide the extended options (the toggled
0117     // signal won't be emitted).
0118 
0119     d->extendedTagsButton->setChecked(true);
0120     d->extendedTagsButton->setSizePolicy(QSizePolicy::Maximum,
0121                                         QSizePolicy::Preferred);
0122 
0123     d->extendedTagsBox               = new QGroupBox(QLatin1String(""), getSettingsBox());
0124     d->extendedTagsBox->setFlat(true);
0125     QGridLayout* extendedTagsLayout  = new QGridLayout(d->extendedTagsBox);
0126 
0127     QLabel* const tagsLabel          = new QLabel(i18n("Added Tags: "), d->extendedTagsBox);
0128     d->tagsLineEdit                  = new DTextEdit(d->extendedTagsBox);
0129     d->tagsLineEdit->setLinesVisible(1);
0130     d->tagsLineEdit->setToolTip(i18n("Enter new tags here, separated by commas."));
0131     d->addExtraTagsCheckBox          = new QCheckBox(d->extendedTagsBox);
0132     d->addExtraTagsCheckBox->setText(i18n("Add tags per image"));
0133     d->addExtraTagsCheckBox->setToolTip(i18n("If checked, you can set extra tags for "
0134                                             "each image in the File List tab"));
0135     d->addExtraTagsCheckBox->setChecked(true);
0136     d->stripSpaceTagsCheckBox        = new QCheckBox(d->extendedTagsBox);
0137     d->stripSpaceTagsCheckBox->setText(i18n("Strip Spaces From Tags"));
0138 
0139     extendedTagsLayout->addWidget(tagsLabel,                 0, 0);
0140     extendedTagsLayout->addWidget(d->tagsLineEdit,           0, 1);
0141     extendedTagsLayout->addWidget(d->stripSpaceTagsCheckBox, 1, 0, 1, 2);
0142     extendedTagsLayout->addWidget(d->addExtraTagsCheckBox,   2, 0, 1, 2);
0143 
0144     tagsBoxLayout->addWidget(d->exportHostTagsCheckBox, 0, 0);
0145     tagsBoxLayout->addWidget(d->extendedTagsButton,     0, 1);
0146     tagsBoxLayout->addWidget(d->extendedTagsBox,        1, 0, 1, 2);
0147 
0148     // -- Layout for the publication options ----------------------------------
0149 
0150     QGroupBox* const publicationBox         = new QGroupBox(i18n("Publication Options"), getSettingsBox());
0151     QGridLayout* const publicationBoxLayout = new QGridLayout;
0152     publicationBox->setLayout(publicationBoxLayout);
0153 
0154     d->publicCheckBox = new QCheckBox(publicationBox);
0155     d->publicCheckBox->setText(i18nc("As in accessible for people", "Public (anyone can see them)"));
0156 
0157     d->familyCheckBox = new QCheckBox(publicationBox);
0158     d->familyCheckBox->setText(i18n("Visible to Family"));
0159 
0160     d->friendsCheckBox = new QCheckBox(publicationBox);
0161     d->friendsCheckBox->setText(i18n("Visible to Friends"));
0162 
0163     // Extended publication settings
0164 
0165     d->extendedPublicationButton = new QPushButton(i18n("More publication options"));
0166     d->extendedPublicationButton->setCheckable(true);
0167 
0168     // Initialize this button to checked, so extended options are shown.
0169     // FlickrWindow::readSettings can change this, but if checked is false it
0170     // cannot uncheck and subsequently hide the extended options (the toggled
0171     // signal won't be emitted).
0172 
0173     d->extendedPublicationButton->setChecked(true);
0174     d->extendedPublicationButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
0175 
0176     d->extendedPublicationBox                  = new QGroupBox(QLatin1String(""), publicationBox);
0177     d->extendedPublicationBox->setFlat(true);
0178     QGridLayout* const extendedSettingsLayout = new QGridLayout(d->extendedPublicationBox);
0179 
0180     QLabel* const imageSafetyLabel = new QLabel(i18n("Safety level:"));
0181     d->safetyLevelComboBox          = new WSComboBoxIntermediate();
0182     d->safetyLevelComboBox->addItem(i18n("Safe"),       QVariant(FlickrList::SAFE));
0183     d->safetyLevelComboBox->addItem(i18n("Moderate"),   QVariant(FlickrList::MODERATE));
0184     d->safetyLevelComboBox->addItem(i18n("Restricted"), QVariant(FlickrList::RESTRICTED));
0185 
0186     QLabel* const imageTypeLabel = new QLabel(i18n("Content type:"));
0187     d->contentTypeComboBox        = new WSComboBoxIntermediate();
0188     d->contentTypeComboBox->addItem(i18nc("photo content type", "Photo"),      QVariant(FlickrList::PHOTO));
0189     d->contentTypeComboBox->addItem(i18nc("photo content type", "Screenshot"), QVariant(FlickrList::SCREENSHOT));
0190     d->contentTypeComboBox->addItem(i18nc("photo content type", "Other"),      QVariant(FlickrList::OTHER));
0191 
0192     extendedSettingsLayout->addWidget(imageSafetyLabel,      1, 0, Qt::AlignLeft);
0193     extendedSettingsLayout->addWidget(d->safetyLevelComboBox, 1, 1, Qt::AlignLeft);
0194     extendedSettingsLayout->addWidget(imageTypeLabel,        0, 0, Qt::AlignLeft);
0195     extendedSettingsLayout->addWidget(d->contentTypeComboBox, 0, 1, Qt::AlignLeft);
0196     extendedSettingsLayout->setColumnStretch(0, 0);
0197     extendedSettingsLayout->setColumnStretch(1, 1);
0198 
0199     publicationBoxLayout->addWidget(d->publicCheckBox,            0, 0);
0200     publicationBoxLayout->addWidget(d->familyCheckBox,            1, 0);
0201     publicationBoxLayout->addWidget(d->friendsCheckBox,           2, 0);
0202     publicationBoxLayout->addWidget(d->extendedPublicationButton, 2, 1);
0203     publicationBoxLayout->addWidget(d->extendedPublicationBox,    3, 0, 1, 2);
0204 
0205     // -- Add these extra widgets to settings box -------------------------------------------------
0206 
0207     addWidgetToSettingsBox(publicationBox);
0208     addWidgetToSettingsBox(tagsBox);
0209 
0210     // hiding widgets not required.
0211 
0212     getUploadBox()->hide();
0213     getSizeBox()->hide();
0214 
0215     // Removing DItemsList inherited from WSSettingsWidget and replacing it with more specific FlickrList
0216 
0217     replaceImageList(d->imglst);
0218 
0219     this->updateLabels();
0220 
0221     connect(d->imglst, SIGNAL(signalPermissionChanged(FlickrList::FieldType,Qt::CheckState)),
0222             this, SLOT(slotPermissionChanged(FlickrList::FieldType,Qt::CheckState)));
0223 
0224     connect(d->publicCheckBox, SIGNAL(stateChanged(int)),
0225             this, SLOT(slotMainPublicToggled(int)));
0226 
0227     connect(d->extendedTagsButton, SIGNAL(toggled(bool)),
0228             this, SLOT(slotExtendedTagsToggled(bool)));
0229 
0230     connect(d->addExtraTagsCheckBox, SIGNAL(toggled(bool)),
0231             this, SLOT(slotAddExtraTagsToggled(bool)));
0232 
0233     connect(d->familyCheckBox, SIGNAL(stateChanged(int)),
0234             this, SLOT(slotMainFamilyToggled(int)));
0235 
0236     connect(d->friendsCheckBox, SIGNAL(stateChanged(int)),
0237             this, SLOT(slotMainFriendsToggled(int)));
0238 
0239     connect(d->safetyLevelComboBox, SIGNAL(currentIndexChanged(int)),
0240             this, SLOT(slotMainSafetyLevelChanged(int)));
0241 
0242     connect(d->contentTypeComboBox, SIGNAL(currentIndexChanged(int)),
0243             this, SLOT(slotMainContentTypeChanged(int)));
0244 
0245     connect(d->extendedPublicationButton, SIGNAL(toggled(bool)),
0246             this, SLOT(slotExtendedPublicationToggled(bool)));
0247 
0248     connect(d->imglst, SIGNAL(signalSafetyLevelChanged(FlickrList::SafetyLevel)),
0249             this, SLOT(slotSafetyLevelChanged(FlickrList::SafetyLevel)));
0250 
0251     connect(d->imglst, SIGNAL(signalContentTypeChanged(FlickrList::ContentType)),
0252             this, SLOT(slotContentTypeChanged(FlickrList::ContentType)));
0253 }
0254 
0255 FlickrWidget::~FlickrWidget()
0256 {
0257     delete d;
0258 }
0259 
0260 void FlickrWidget::updateLabels(const QString& /*name*/, const QString& /*url*/)
0261 {
0262     getHeaderLbl()->setText(i18n("<b><h2><a href='https://www.flickr.com'>"
0263                                  "<font color=\"#0065DE\">flick</font>"
0264                                  "<font color=\"#FF0084\">r</font></a>"
0265                                  " Export"
0266                                  "</h2></b>"));
0267 }
0268 
0269 void FlickrWidget::slotPermissionChanged(FlickrList::FieldType checkbox, Qt::CheckState state)
0270 {
0271     /*
0272      * Slot for handling the signal from the FlickrList that the general
0273      * permissions have changed, considering the clicks in the checkboxes next
0274      * to each image. In response, the main permission checkboxes should be set
0275      * to the proper state.
0276      * The checkbox variable determines which of the checkboxes should be
0277      * changed.
0278      */
0279 
0280     // Select the proper checkbox.
0281 
0282     QCheckBox* currBox = nullptr;
0283 
0284     if      (checkbox == FlickrList::PUBLIC)
0285     {
0286         currBox = d->publicCheckBox;
0287     }
0288     else if (checkbox == FlickrList::FAMILY)
0289     {
0290         currBox = d->familyCheckBox;
0291     }
0292     else
0293     {
0294         currBox = d->friendsCheckBox;
0295     }
0296 
0297     // If the checkbox should be set in the intermediate state, the tristate
0298     // property of the checkbox should be manually set to true, otherwise, it
0299     // has to be set to false so that the user cannot select it.
0300 
0301     currBox->setCheckState(state);
0302 
0303     if ((state == Qt::Checked) || (state == Qt::Unchecked))
0304     {
0305         currBox->setTristate(false);
0306     }
0307     else
0308     {
0309         currBox->setTristate(true);
0310     }
0311 }
0312 
0313 void FlickrWidget::slotSafetyLevelChanged(FlickrList::SafetyLevel safetyLevel)
0314 {
0315     /*
0316      * Called when the general safety level of the FlickrList has changed,
0317      * considering the individual comboboxes next to the photos. Used to set the
0318      * main safety level combobox appropriately.
0319      */
0320 
0321     if (safetyLevel == FlickrList::MIXEDLEVELS)
0322     {
0323         d->safetyLevelComboBox->setIntermediate(true);
0324     }
0325     else
0326     {
0327         int index = d->safetyLevelComboBox->findData(QVariant(static_cast<int>(safetyLevel)));
0328         d->safetyLevelComboBox->setCurrentIndex(index);
0329     }
0330 }
0331 
0332 void FlickrWidget::slotContentTypeChanged(FlickrList::ContentType contentType)
0333 {
0334     /*
0335      * Called when the general content type of the FlickrList has changed,
0336      * considering the individual comboboxes next to the photos. Used to set the
0337      * main content type combobox appropriately.
0338      */
0339 
0340     if (contentType == FlickrList::MIXEDTYPES)
0341     {
0342         d->contentTypeComboBox->setIntermediate(true);
0343     }
0344     else
0345     {
0346         int index = d->contentTypeComboBox->findData(QVariant(static_cast<int>(contentType)));
0347         d->contentTypeComboBox->setCurrentIndex(index);
0348     }
0349 }
0350 
0351 void FlickrWidget::slotMainPublicToggled(int state)
0352 {
0353     mainPermissionToggled(FlickrList::PUBLIC, static_cast<Qt::CheckState>(state));
0354 }
0355 
0356 void FlickrWidget::slotMainFamilyToggled(int state)
0357 {
0358     mainPermissionToggled(FlickrList::FAMILY, static_cast<Qt::CheckState>(state));
0359 }
0360 
0361 void FlickrWidget::slotMainFriendsToggled(int state)
0362 {
0363     mainPermissionToggled(FlickrList::FRIENDS, static_cast<Qt::CheckState>(state));
0364 }
0365 
0366 void FlickrWidget::mainPermissionToggled(FlickrList::FieldType checkbox, Qt::CheckState state)
0367 {
0368     /*
0369      * Callback for when one of the main permission checkboxes is toggled.
0370      * checkbox specifies which of the checkboxes is toggled.
0371      */
0372 
0373     if (state != Qt::PartiallyChecked)
0374     {
0375         // Set the states for the image list.
0376 
0377         if      (checkbox == FlickrList::PUBLIC)
0378         {
0379             d->imglst->setPublic(state);
0380         }
0381         else if (checkbox == FlickrList::FAMILY)
0382         {
0383             d->imglst->setFamily(state);
0384         }
0385         else if (checkbox == FlickrList::FRIENDS)
0386         {
0387             d->imglst->setFriends(state);
0388         }
0389 
0390         // Dis- or enable the family and friends checkboxes if the public
0391         // checkbox is clicked.
0392 
0393         if (checkbox == 0)
0394         {
0395             if      (state == Qt::Checked)
0396             {
0397                 d->familyCheckBox->setEnabled(false);
0398                 d->friendsCheckBox->setEnabled(false);
0399             }
0400             else if (state == Qt::Unchecked)
0401             {
0402                 d->familyCheckBox->setEnabled(true);
0403                 d->friendsCheckBox->setEnabled(true);
0404             }
0405         }
0406 
0407         // Set the main checkbox tristate state to false, so that the user
0408         // cannot select the intermediate state.
0409 
0410         if      (checkbox == FlickrList::PUBLIC)
0411         {
0412             d->publicCheckBox->setTristate(false);
0413         }
0414         else if (checkbox == FlickrList::FAMILY)
0415         {
0416             d->familyCheckBox->setTristate(false);
0417         }
0418         else if (checkbox == FlickrList::FRIENDS)
0419         {
0420             d->friendsCheckBox->setTristate(false);
0421         }
0422     }
0423 }
0424 
0425 void FlickrWidget::slotMainSafetyLevelChanged(int index)
0426 {
0427     int currValue = (d->safetyLevelComboBox->itemData(index)).value<int>();
0428 /*
0429      int currValue = qVariantValue<int>(d->safetyLevelComboBox->itemData(index));
0430 */
0431     d->imglst->setSafetyLevels(static_cast<FlickrList::SafetyLevel>(currValue));
0432 }
0433 
0434 void FlickrWidget::slotMainContentTypeChanged(int index)
0435 {
0436     int currValue = (d->contentTypeComboBox->itemData(index)).value<int>();
0437 /*
0438      int currValue = qVariantValue<int>(d->contentTypeComboBox->itemData(index));
0439 */
0440     d->imglst->setContentTypes(static_cast<FlickrList::ContentType>(currValue));
0441 }
0442 
0443 void FlickrWidget::slotExtendedPublicationToggled(bool status)
0444 {
0445     // Show or hide the extended settings when the extended settings button
0446     // is toggled.
0447 
0448     d->extendedPublicationBox->setVisible(status);
0449     d->imglst->listView()->setColumnHidden(FlickrList::SAFETYLEVEL, !status);
0450     d->imglst->listView()->setColumnHidden(FlickrList::CONTENTTYPE, !status);
0451 
0452     if (status)
0453     {
0454         d->extendedPublicationButton->setText(i18n("Fewer publication options"));
0455     }
0456     else
0457     {
0458         d->extendedPublicationButton->setText(i18n("More publication options"));
0459     }
0460 }
0461 
0462 void FlickrWidget::slotExtendedTagsToggled(bool status)
0463 {
0464     // Show or hide the extended tag settings when the extended tag option
0465     // button is toggled.
0466 
0467     d->extendedTagsBox->setVisible(status);
0468 
0469     if (!status)
0470     {
0471         d->imglst->listView()->setColumnHidden(FlickrList::TAGS, true);
0472         d->extendedTagsButton->setText(i18n("More tag options"));
0473     }
0474     else
0475     {
0476         d->imglst->listView()->setColumnHidden(FlickrList::TAGS, !d->addExtraTagsCheckBox->isChecked());
0477         d->extendedTagsButton->setText(i18n("Fewer tag options"));
0478     }
0479 }
0480 
0481 void FlickrWidget::slotAddExtraTagsToggled(bool status)
0482 {
0483     if (d->extendedTagsButton->isChecked())
0484     {
0485         d->imglst->listView()->setColumnHidden(FlickrList::TAGS, !status);
0486     }
0487 }
0488 
0489 } // namespace DigikamGenericFlickrPlugin
0490 
0491 #include "moc_flickrwidget.cpp"