Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-03 Thread Jason H


> Sent: Wednesday, March 02, 2016 at 6:26 PM
> From: "Thiago Macieira" 
> To: interest@qt-project.org
> Subject: Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) 
> QIODevice?
>
> On quinta-feira, 3 de março de 2016 00:08:38 PST Jason H wrote:
> > > Another idea: why don't you use QTemporaryFile?
> > 
> > Thought about it. It takes a template specification which does not allow me
> > to specify the exact filename. "If the templateName does not contain XX
> > it will automatically be appended and used as the dynamic portion of the
> > filename."
> 
> Why do you need a specific name?
> 
> What happens if another application needs that same name at the same time? 
> Will you delete the other application's file?

The filename is returned by a function which encodes the primary key as the 
filename, which then is used to mark the item as done when the result is http 
status code is 200. Without it, I have to track the file item mapping to 
filename somehow. 

As for why I am doing all of that, I am implementing a Qt replacement for 
iOS-like NSURLSession delegate which requires background uploads to be written 
to a file. I set the taskDescription to the filename, grab it back in 
didCompelteWithError, where I update the database and delete the file. The Qt 
version does not have a description field, so I *have* to use the file name or 
some other mapping. 

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-02 Thread Thiago Macieira
On quinta-feira, 3 de março de 2016 00:08:38 PST Jason H wrote:
> > Another idea: why don't you use QTemporaryFile?
> 
> Thought about it. It takes a template specification which does not allow me
> to specify the exact filename. "If the templateName does not contain XX
> it will automatically be appended and used as the dynamic portion of the
> filename."

Why do you need a specific name?

What happens if another application needs that same name at the same time? 
Will you delete the other application's file?
-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-02 Thread Jason H

> Another idea: why don't you use QTemporaryFile?

Thought about it. It takes a template specification which does not allow me to 
specify the exact filename. "If the templateName does not contain XX it 
will automatically be appended and used as the dynamic portion of the filename."
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-02 Thread Thiago Macieira
On quarta-feira, 2 de março de 2016 09:53:46 PST Thiago Macieira wrote:
> On quarta-feira, 2 de março de 2016 17:45:15 PST Jason H wrote:
> > Let me expand on this, I can f->setParent(reply), but I also need to
> > delete
> > the file from disk. If I rely on the parent/child object deletion, I can't
> > get a change to delete the file.
> 
> If you're on Unix, you can delete the file while it's still open. The file
> will disappear from the directory listing immediately and the contents will
> be discarded once the  last open file descriptor closes.

Another idea: why don't you use QTemporaryFile?

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-02 Thread Julien Cugnière
And yet another idea (untested), using C++11 lambdas :

QFile *f = new QFile(filename);
f->open(QIODevice::ReadOnly);

QNetworkReply* reply = nam.post(req, f);

connect(reply, &QNetworkReply::finished, f, [=f]() {
f->close();
f->remove();
delete f; // or f->deleteLater()
});


Julien Cugnière

2016-03-02 19:19 GMT+01:00 Elvis Stansvik :
>
> 2016-03-02 18:56 GMT+01:00 Jason H :
> > Indeed it is, there's a couple ways to skin this cat.
>
> Another idea, after looking at the API: You could perhaps transport
> your QFile* pointer in a user attribute of the request, e.g. something
> like (untested):
>
> int CoolFileAttribute = QNetworkRequest::User;
>
> // Sending the request, attaching the QFile* pointer to it.
> QFile *f = new QFile(filename);
> f->open(QIODevice::ReadOnly)
> ...
> req.setAttribute(CoolFileAttribute, QVariant::fromValue(f));
> nam.post(req, f);
>
> // And in your slot handling the reply, get the attribute from the
> corresponding request.
> QFile *f = reply->request().attribute(CoolFileAttribute).value();
> f->remove();
> f->close();
> delete f;
>
> With obviously some improved naming / error handling.
>
> Elvis
>
> > What I settled on (so far) is since I'm using setParent(), isto iterate over
> > the children and just QFile::remove() anything that can be cast as a QFile*.
> > It eliminates maintaining an additional mapping. it seems that this idea of
> > having a file that is to be deleted on request does not exist, except for
> > QTemporaryFile which you can't explitily state the filename.
> >
> >> Let me expand on this, I can f->setParent(reply), but I also need to
> >> delete the file from disk. If I rely on the parent/child object deletion, I
> >> can't get a change to delete the file.
> >>
> >> > QFile *f = new QFile(filename);
> >> > f->open(QIODevice::ReadOnly)
> >> > ...
> >> > nam.post(req, f);
> >> > ...
> >> >
> >> > // sometime later in the finished slot:
> >> > finished(QNetworkReply *reply) {
> >> >
> >> >  // how to close and delete f ? (The QFile object)
> >
> > Save the pointer somewhere when you create it, then use the saved pointer to
> > close/delete it?
> >
> > I guess you would need to store it indexed by the request, so that you know
> > which file to close/delete in your slot.
> >
> > Take my advice with some grain of salt, as I haven't used QNAM before. But
> > it sounds like a general resource management problem.
> >
> > Cheers,
> > Elvis
> >
> >
> ___
> Interest mailing list
> Interest@qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-02 Thread Elvis Stansvik
2016-03-02 18:56 GMT+01:00 Jason H :
> Indeed it is, there's a couple ways to skin this cat.

Another idea, after looking at the API: You could perhaps transport
your QFile* pointer in a user attribute of the request, e.g. something
like (untested):

int CoolFileAttribute = QNetworkRequest::User;

// Sending the request, attaching the QFile* pointer to it.
QFile *f = new QFile(filename);
f->open(QIODevice::ReadOnly)
...
req.setAttribute(CoolFileAttribute, QVariant::fromValue(f));
nam.post(req, f);

// And in your slot handling the reply, get the attribute from the
corresponding request.
QFile *f = reply->request().attribute(CoolFileAttribute).value();
f->remove();
f->close();
delete f;

With obviously some improved naming / error handling.

Elvis

> What I settled on (so far) is since I'm using setParent(), isto iterate over
> the children and just QFile::remove() anything that can be cast as a QFile*.
> It eliminates maintaining an additional mapping. it seems that this idea of
> having a file that is to be deleted on request does not exist, except for
> QTemporaryFile which you can't explitily state the filename.
>
>> Let me expand on this, I can f->setParent(reply), but I also need to
>> delete the file from disk. If I rely on the parent/child object deletion, I
>> can't get a change to delete the file.
>>
>> > QFile *f = new QFile(filename);
>> > f->open(QIODevice::ReadOnly)
>> > ...
>> > nam.post(req, f);
>> > ...
>> >
>> > // sometime later in the finished slot:
>> > finished(QNetworkReply *reply) {
>> >
>> >  // how to close and delete f ? (The QFile object)
>
> Save the pointer somewhere when you create it, then use the saved pointer to
> close/delete it?
>
> I guess you would need to store it indexed by the request, so that you know
> which file to close/delete in your slot.
>
> Take my advice with some grain of salt, as I haven't used QNAM before. But
> it sounds like a general resource management problem.
>
> Cheers,
> Elvis
>
>
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-02 Thread Thiago Macieira
On quarta-feira, 2 de março de 2016 17:45:15 PST Jason H wrote:
> Let me expand on this, I can f->setParent(reply), but I also need to delete
> the file from disk. If I rely on the parent/child object deletion, I can't
> get a change to delete the file.

If you're on Unix, you can delete the file while it's still open. The file will 
disappear from the directory listing immediately and the contents will be 
discarded once the  last open file descriptor closes.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-02 Thread Jason H
Let me expand on this, I can f->setParent(reply), but I also need to delete the 
file from disk. If I rely on the parent/child object deletion, I can't get a 
change to delete the file.

> QFile *f = new QFile(filename);
> f->open(QIODevice::ReadOnly) 
> ...
> nam.post(req, f);
> ...
> 
> // sometime later in the finished slot:
> finished(QNetworkReply *reply) {
> 
>  // how to close and delete f ? (The QFile object)
> 
> }
>
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


[Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

2016-03-02 Thread Jason H

QFile *f = new QFile(filename);
f->open(QIODevice::ReadOnly) 
...
nam.post(req, f);
...

// sometime later in the finished slot:
finished(QNetworkReply *reply) {

 // how to close and delete f ? (The QFile object)

}

Thanks
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest