Changeset View
Standalone View
src/trash/dolphintrash.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 by Roman Inflianskas <infroma@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program; if not, write to the * | ||||
16 | * Free Software Foundation, Inc., * | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||||
18 | ***************************************************************************/ | ||||
19 | | ||||
20 | #include "dolphintrash.h" | ||||
21 | | ||||
22 | #include <KIO/JobUiDelegate> | ||||
23 | #include <KJobWidgets> | ||||
24 | #include <QList> | ||||
25 | #include <KNotification> | ||||
26 | #include <KConfig> | ||||
27 | #include <KConfigGroup> | ||||
28 | #include <KIOWidgets/KDirLister> | ||||
29 | | ||||
30 | | ||||
31 | Trash& Trash::instance() | ||||
32 | { | ||||
33 | static Trash result; | ||||
34 | return result; | ||||
35 | } | ||||
36 | | ||||
37 | KIO::Job *Trash::empty(QWidget *window) const | ||||
38 | { | ||||
39 | KIO::JobUiDelegate uiDelegate; | ||||
40 | uiDelegate.setWindow(window); | ||||
41 | bool confirmed = uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation); | ||||
42 | if (confirmed) { | ||||
43 | KIO::Job* job = KIO::emptyTrash(); | ||||
44 | KJobWidgets::setWindow(job, window); | ||||
45 | job->uiDelegate()->setAutoErrorHandlingEnabled(true); | ||||
46 | connect(job, &KIO::Job::result, [](){ | ||||
47 | KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), nullptr, KNotification::DefaultEvent); | ||||
48 | }); | ||||
49 | return job; | ||||
50 | } | ||||
markg: I see this was there before (in a different location), but i just don't get this part. | |||||
@markg https://lxr.kde.org/source/frameworks/kio/src/core/kfileitem.cpp#0883: // We need to find if the trash is empty, preferably without using a KIO job. // So instead kio_trash leaves an entry in its config file for us. Cannot comment on whether that's only applicable for KIO or Dolphin too. rkflx: @markg https://lxr.kde.org/source/frameworks/kio/src/core/kfileitem.cpp#0883:
// We need to… | |||||
Ahh, that makes sense. Thank you for the pointer! No wonder i couldn't find it in Dolphin, it wasn't there ;) markg: Ahh, that makes sense.
If dolphin only needed the icon it wouldn't need to look at that file… | |||||
51 | return nullptr; | ||||
52 | } | ||||
You probably don't want to do that. As a mere setting would decide if the trash is empty or not.
Spoiler: void PlacesItem::onTrashDirListerCompleted() { Q_ASSERT(url().scheme() == QLatin1String("trash")); const bool isTrashEmpty = m_trashDirLister->items().isEmpty(); setIcon(isTrashEmpty ? QStringLiteral("user-trash") : QStringLiteral("user-trash-full")); } Also, you already have the information if the trash is empty. You have the view status information (the thingy below the view that says "x Folders, X files". Follow that to figure out how it fills that information which would also give you the trash state. This would be the way to go imho. markg: You probably don't want to do that. As a mere setting would decide if the trash is empty or not. | |||||
@rominf Did you look at Mark's comment already? rkflx: @rominf Did you look at Mark's comment already? | |||||
This code doesn't work well. See https://bugs.kde.org/show_bug.cgi?id=252483
This way is not my idea. I asked David Faure, the KIO maintainer. He said that this way is used everywhere (it's used in Dolphin too). He also said that because of that it's a nice idea to put it to KIO. rominf: > Look at placesitem.cpp at the "m_trashDirLister" how that is used to update the trash icon in… | |||||
53 | | ||||
54 | bool Trash::isEmpty() const | ||||
55 | { | ||||
56 | KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig); | ||||
57 | return (trashConfig.group("Status").readEntry("Empty", true)); | ||||
58 | } | ||||
markg: You now have a memory leak. The trashDirLister is never deleted. | |||||
59 | | ||||
60 | void Trash::watchEmptinessChanged() | ||||
61 | { | ||||
markg: missing space after the comma. | |||||
62 | // The trash icon must always be updated dependent on whether | ||||
Don't overuse auto. It makes sense for the lambda above and for otherwise link complicated types. In this case, just use bool. markg: Don't overuse auto. It makes sense for the lambda above and for otherwise link complicated… | |||||
63 | // the trash is empty or not. We use a KDirLister that automatically | ||||
64 | // watches for changes if the number of items has been changed. | ||||
65 | // The update of the icon is handled in onTrashEmptinessChanged(). | ||||
66 | m_trashDirLister = new KDirLister(); | ||||
67 | m_trashDirLister->setAutoErrorHandlingEnabled(false, nullptr); | ||||
This - as i said in my last message - smells like a bug. If that is required then KIO likely has a bug, Don't fix this here, hunt for the issue in KIO please. markg: This - as i said in my last message - smells like a bug. If that is required then KIO likely… | |||||
I understand that this is a bug. I've tried to fix it but I didn't understand how does KDirLister work. rominf: I understand that this is a bug. I've tried to fix it but I didn't understand how does… | |||||
I rather not do that. Quick fixes (nearly) always stay in for far too long or people just forget about it altogether because it "seemingly works". If i haven't found a fix for it by the end of sunday then i'm not going to find one anytime soon. So: wait till sunday. If i found a fix, use it. markg: I rather not do that. Quick fixes (nearly) always stay in for far too long or people just… | |||||
rominf: OK. I agree. | |||||
rominf: Did you find/fix a bug? | |||||
68 | m_trashDirLister->setDelayedMimeTypes(true); | ||||
69 | auto trashDirContentChanged = [this,m_trashDirLister](){ | ||||
70 | bool isTrashEmpty = m_trashDirLister->items().isEmpty(); | ||||
71 | emit emptinessChanged(isTrashEmpty); | ||||
72 | }; | ||||
73 | connect(m_trashDirLister, static_cast<void(KDirLister::*)()>(&KDirLister::completed), trashDirContentChanged); | ||||
74 | // This is bug in KIO. KDirLister::completed should fire up when KDirLister::itemsDeleted is fired. | ||||
75 | // This connect is workaround. If you find a way how to fix KIO, DO IT and remove this connect. | ||||
76 | connect(m_trashDirLister, static_cast<void(KDirLister::*)(const KFileItemList&)>(&KDirLister::itemsDeleted), | ||||
77 | trashDirContentChanged); | ||||
78 | m_trashDirLister->openUrl(QStringLiteral("trash:/")); | ||||
79 | } | ||||
80 | | ||||
81 | Trash::Trash() | ||||
82 | { | ||||
83 | watchEmptinessChanged(); | ||||
84 | } | ||||
85 | | ||||
86 | Trash::~Trash() | ||||
87 | { | ||||
88 | delete m_trashDirLister; | ||||
89 | } |
I see this was there before (in a different location), but i just don't get this part. "trashrc" is only used here (I've searched through the code) and it looks like nothing sets it hence the default is used which is true which in turn indicates the trash is empty.
Why?
This just doesn't make sense to me.
Or.. i can't search and it is used elsewhere, but then i didn't find it.