~ubuntu-branches/ubuntu/trusty/nzbget/trusty

« back to all changes in this revision

Viewing changes to QueueEditor.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Moog
  • Date: 2013-07-18 14:50:28 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130718145028-48qbia23dx6sg8u1
Tags: 11.0+dfsg-0ubuntu1
* Repackage original tarball to remove copies of jquery and twitter-
  bootstrap
* debian/watch: Update for new versioning scheme
* debian/patches: Remove all old patches, add one patch:
  - dont-embed-libraries.patch: Don't install embedded jquery and bootstrap 
    libraries
* debian/combat: Upgrade to debhelper combat 9
* debian/control:
  - Fix Vcs-Git field
  - Adjust debhelper version for combat level 9
  - Add jquery and bootstrap to depends for integrated webserver
  - Add python to recommends for post-processing scripts
  - Bump build-depends on libpar2-dev to support the cancel function
* debian/links:
  - Use the system jquery and bootstrap libraries
* debian/rules:
  - Add get-orig-source target to build modified upstream tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  This file is part of nzbget
3
3
 *
4
 
 *  Copyright (C) 2007-2009 Andrei Prygounkov <hugbug@users.sourceforge.net>
 
4
 *  Copyright (C) 2007-2013 Andrey Prygunkov <hugbug@users.sourceforge.net>
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  along with this program; if not, write to the Free Software
18
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
19
 *
20
 
 * $Revision: 320 $
21
 
 * $Date: 2009-05-24 16:26:53 +0200 (Sun, 24 May 2009) $
 
20
 * $Revision: 677 $
 
21
 * $Date: 2013-05-14 22:20:52 +0200 (Tue, 14 May 2013) $
22
22
 *
23
23
 */
24
24
 
25
25
 
26
26
#ifdef HAVE_CONFIG_H
27
 
#include <config.h>
 
27
#include "config.h"
28
28
#endif
29
29
 
30
30
#ifdef WIN32
33
33
 
34
34
#include <stdlib.h>
35
35
#include <string.h>
36
 
#include <cctype>
37
 
#include <cstdio>
 
36
#include <stdio.h>
 
37
#include <ctype.h>
38
38
#include <sys/stat.h>
 
39
#include <set>
39
40
#ifndef WIN32
40
41
#include <unistd.h>
41
42
#include <sys/time.h>
110
111
}
111
112
 
112
113
/*
113
 
 * Removes entry with index iEntry
 
114
 * Removes entry
114
115
 * returns true if successful, false if operation is not possible
115
116
 */
116
117
void QueueEditor::DeleteEntry(FileInfo* pFileInfo)
120
121
}
121
122
 
122
123
/*
123
 
 * Moves entry identified with iID in the queue
 
124
 * Moves entry in the queue
124
125
 * returns true if successful, false if operation is not possible
125
126
 */
126
127
void QueueEditor::MoveEntry(DownloadQueue* pDownloadQueue, FileInfo* pFileInfo, int iOffset)
148
149
        }
149
150
}
150
151
 
 
152
/*
 
153
 * Set priority for entry
 
154
 * returns true if successful, false if operation is not possible
 
155
 */
 
156
void QueueEditor::SetPriorityEntry(FileInfo* pFileInfo, const char* szPriority)
 
157
{
 
158
        debug("Setting priority %s for file %s", szPriority, pFileInfo->GetFilename());
 
159
        int iPriority = atoi(szPriority);
 
160
        pFileInfo->SetPriority(iPriority);
 
161
}
 
162
 
151
163
bool QueueEditor::EditEntry(int ID, bool bSmartOrder, EEditAction eAction, int iOffset, const char* szText)
152
164
{
153
165
        IDList cIDList;
154
166
        cIDList.clear();
155
167
        cIDList.push_back(ID);
156
 
        return EditList(&cIDList, bSmartOrder, eAction, iOffset, szText);
 
168
        return EditList(&cIDList, NULL, mmID, bSmartOrder, eAction, iOffset, szText);
157
169
}
158
170
 
159
171
bool QueueEditor::LockedEditEntry(DownloadQueue* pDownloadQueue, int ID, bool bSmartOrder, EEditAction eAction, int iOffset, const char* szText)
164
176
        return InternEditList(pDownloadQueue, &cIDList, bSmartOrder, eAction, iOffset, szText);
165
177
}
166
178
 
167
 
bool QueueEditor::EditList(IDList* pIDList, bool bSmartOrder, EEditAction eAction, int iOffset, const char* szText)
 
179
bool QueueEditor::EditList(IDList* pIDList, NameList* pNameList, EMatchMode eMatchMode, bool bSmartOrder, 
 
180
        EEditAction eAction, int iOffset, const char* szText)
168
181
{
169
182
        DownloadQueue* pDownloadQueue = g_pQueueCoordinator->LockQueue();
170
183
 
171
 
        bool bOK = InternEditList(pDownloadQueue, pIDList, bSmartOrder, eAction, iOffset, szText);
 
184
        bool bOK = true;
 
185
 
 
186
        if (pNameList)
 
187
        {
 
188
                pIDList = new IDList();
 
189
                bOK = BuildIDListFromNameList(pDownloadQueue, pIDList, pNameList, eMatchMode, eAction);
 
190
        }
 
191
 
 
192
        bOK = bOK && (InternEditList(pDownloadQueue, pIDList, bSmartOrder, eAction, iOffset, szText) || eMatchMode == mmRegEx);
172
193
 
173
194
        if (g_pOptions->GetSaveQueue() && g_pOptions->GetServerMode())
174
195
        {
177
198
 
178
199
        g_pQueueCoordinator->UnlockQueue();
179
200
 
 
201
        if (pNameList)
 
202
        {
 
203
                delete pIDList;
 
204
        }
 
205
 
180
206
        return bOK;
181
207
}
182
208
 
201
227
        }
202
228
        else if (eAction == eaGroupMerge)
203
229
        {
204
 
                MergeGroups(pDownloadQueue, &cItemList);
 
230
                return MergeGroups(pDownloadQueue, &cItemList);
 
231
        }
 
232
        else if (eAction == eaFileSplit)
 
233
        {
 
234
                return SplitGroup(pDownloadQueue, &cItemList, szText);
 
235
        }
 
236
        else if (eAction == eaFileReorder)
 
237
        {
 
238
                ReorderFiles(pDownloadQueue, &cItemList);
205
239
        }
206
240
        else
207
241
        {
228
262
                                        DeleteEntry(pItem->m_pFileInfo);
229
263
                                        break;
230
264
 
 
265
                                case eaFileSetPriority:
 
266
                                        SetPriorityEntry(pItem->m_pFileInfo, szText);
 
267
                                        break;
 
268
 
231
269
                                case eaGroupSetCategory:
232
270
                                        SetNZBCategory(pItem->m_pFileInfo->GetNZBInfo(), szText);
233
271
                                        break;
234
272
 
 
273
                                case eaGroupSetName:
 
274
                                        SetNZBName(pItem->m_pFileInfo->GetNZBInfo(), szText);
 
275
                                        break;
 
276
 
235
277
                                case eaGroupSetParameter:
236
278
                                        SetNZBParameter(pItem->m_pFileInfo->GetNZBInfo(), szText);
237
279
                                        break;
244
286
                                case eaGroupMoveOffset:
245
287
                                case eaGroupPauseAllPars:
246
288
                                case eaGroupPauseExtraPars:
247
 
                                        EditGroup(pDownloadQueue, pItem->m_pFileInfo, eAction, iOffset);
 
289
                                case eaGroupSetPriority:
 
290
                                        EditGroup(pDownloadQueue, pItem->m_pFileInfo, eAction, iOffset, szText);
248
291
                                        break;
249
292
 
250
293
                                case eaFilePauseAllPars:
251
294
                                case eaFilePauseExtraPars:
252
295
                                case eaGroupMerge:
 
296
                                case eaFileReorder:
 
297
                                case eaFileSplit:
253
298
                                        // remove compiler warning "enumeration not handled in switch"
254
299
                                        break;
255
300
                        }
365
410
        }
366
411
}
367
412
 
368
 
bool QueueEditor::EditGroup(DownloadQueue* pDownloadQueue, FileInfo* pFileInfo, EEditAction eAction, int iOffset)
 
413
bool QueueEditor::BuildIDListFromNameList(DownloadQueue* pDownloadQueue, IDList* pIDList, NameList* pNameList, EMatchMode eMatchMode, EEditAction eAction)
 
414
{
 
415
#ifndef HAVE_REGEX_H
 
416
        if (eMatchMode == mmRegEx)
 
417
        {
 
418
                return false;
 
419
        }
 
420
#endif
 
421
 
 
422
        std::set<int> uniqueIDs;
 
423
 
 
424
        for (NameList::iterator it = pNameList->begin(); it != pNameList->end(); it++)
 
425
        {
 
426
                const char* szName = *it;
 
427
 
 
428
                RegEx *pRegEx = NULL;
 
429
                if (eMatchMode == mmRegEx)
 
430
                {
 
431
                        pRegEx = new RegEx(szName);
 
432
                        if (!pRegEx->IsValid())
 
433
                        {
 
434
                                delete pRegEx;
 
435
                                return false;
 
436
                        }
 
437
                }
 
438
 
 
439
                bool bFound = false;
 
440
 
 
441
                for (FileQueue::iterator it2 = pDownloadQueue->GetFileQueue()->begin(); it2 != pDownloadQueue->GetFileQueue()->end(); it2++)
 
442
                {
 
443
                        FileInfo* pFileInfo = *it2;
 
444
                        if (eAction < eaGroupMoveOffset)
 
445
                        {
 
446
                                // file action
 
447
                                char szFilename[MAX_PATH];
 
448
                                snprintf(szFilename, sizeof(szFilename) - 1, "%s/%s", pFileInfo->GetNZBInfo()->GetName(), Util::BaseFileName(pFileInfo->GetFilename()));
 
449
                                if (((!pRegEx && !strcmp(szFilename, szName)) || (pRegEx && pRegEx->Match(szFilename))) &&
 
450
                                        (uniqueIDs.find(pFileInfo->GetID()) == uniqueIDs.end()))
 
451
                                {
 
452
                                        uniqueIDs.insert(pFileInfo->GetID());
 
453
                                        pIDList->push_back(pFileInfo->GetID());
 
454
                                        bFound = true;
 
455
                                }
 
456
                        }
 
457
                        else
 
458
                        {
 
459
                                // group action
 
460
                                const char *szFilename = pFileInfo->GetNZBInfo()->GetName();
 
461
                                if (((!pRegEx && !strcmp(szFilename, szName)) || (pRegEx && pRegEx->Match(szFilename))) &&
 
462
                                        (uniqueIDs.find(pFileInfo->GetNZBInfo()->GetID()) == uniqueIDs.end()))
 
463
                                {
 
464
                                        uniqueIDs.insert(pFileInfo->GetNZBInfo()->GetID());
 
465
                                        pIDList->push_back(pFileInfo->GetID());
 
466
                                        bFound = true;
 
467
                                }
 
468
                        }
 
469
                }
 
470
 
 
471
                if (pRegEx)
 
472
                {
 
473
                        delete pRegEx;
 
474
                }
 
475
 
 
476
                if (!bFound && (eMatchMode == mmName))
 
477
                {
 
478
                        return false;
 
479
                }
 
480
        }
 
481
 
 
482
        return true;
 
483
}
 
484
 
 
485
bool QueueEditor::EditGroup(DownloadQueue* pDownloadQueue, FileInfo* pFileInfo, EEditAction eAction, int iOffset, const char* szText)
369
486
{
370
487
        IDList cIDList;
371
488
        cIDList.clear();
431
548
                pFileInfo->GetNZBInfo()->SetCleanupDisk(CanCleanupDisk(pDownloadQueue, pFileInfo->GetNZBInfo()));
432
549
        }
433
550
 
434
 
        EEditAction GroupToFileMap[] = { (EEditAction)0, eaFileMoveOffset, eaFileMoveTop, eaFileMoveBottom, eaFilePause, eaFileResume, eaFileDelete, eaFilePauseAllPars, eaFilePauseExtraPars,
435
 
                eaFileMoveOffset, eaFileMoveTop, eaFileMoveBottom, eaFilePause, eaFileResume, eaFileDelete, eaFilePauseAllPars, eaFilePauseExtraPars, (EEditAction)0, (EEditAction)0, (EEditAction)0 };
 
551
        EEditAction GroupToFileMap[] = { (EEditAction)0, eaFileMoveOffset, eaFileMoveTop, eaFileMoveBottom, eaFilePause,
 
552
                eaFileResume, eaFileDelete, eaFilePauseAllPars, eaFilePauseExtraPars, eaFileSetPriority, eaFileReorder, eaFileSplit,
 
553
                eaFileMoveOffset, eaFileMoveTop, eaFileMoveBottom, eaFilePause, eaFileResume, eaFileDelete, 
 
554
                eaFilePauseAllPars, eaFilePauseExtraPars, eaFileSetPriority,
 
555
                (EEditAction)0, (EEditAction)0, (EEditAction)0 };
436
556
 
437
 
        return InternEditList(pDownloadQueue, &cIDList, true, GroupToFileMap[eAction], iOffset, NULL);
 
557
        return InternEditList(pDownloadQueue, &cIDList, true, GroupToFileMap[eAction], iOffset, szText);
438
558
}
439
559
 
440
560
void QueueEditor::BuildGroupList(DownloadQueue* pDownloadQueue, FileList* pGroupList)
533
653
        for (FileList::iterator it = cAffectedGroupList.begin(); it != cAffectedGroupList.end(); it++)
534
654
        {
535
655
                FileInfo* pFileInfo = *it;
536
 
                AlignGroup(pDownloadQueue, pFileInfo);
 
656
                AlignGroup(pDownloadQueue, pFileInfo->GetNZBInfo());
537
657
        }
538
658
}
539
659
 
540
 
void QueueEditor::AlignGroup(DownloadQueue* pDownloadQueue, FileInfo* pFirstFileInfo)
 
660
void QueueEditor::AlignGroup(DownloadQueue* pDownloadQueue, NZBInfo* pNZBInfo)
541
661
{
542
662
        FileInfo* pLastFileInfo = NULL;
543
663
        unsigned int iLastNum = 0;
545
665
        while (iNum < pDownloadQueue->GetFileQueue()->size())
546
666
        {
547
667
                FileInfo* pFileInfo = pDownloadQueue->GetFileQueue()->at(iNum);
548
 
                if (pFirstFileInfo->GetNZBInfo() == pFileInfo->GetNZBInfo())
 
668
                if (pFileInfo->GetNZBInfo() == pNZBInfo)
549
669
                {
550
670
                        if (pLastFileInfo && iNum - iLastNum > 1)
551
671
                        {
687
807
        g_pQueueCoordinator->SetQueueEntryNZBCategory(pNZBInfo, szCategory);
688
808
}
689
809
 
 
810
void QueueEditor::SetNZBName(NZBInfo* pNZBInfo, const char* szName)
 
811
{
 
812
        debug("QueueEditor: renaming '%s' to '%s'", Util::BaseFileName(pNZBInfo->GetFilename()), szName);
 
813
 
 
814
        g_pQueueCoordinator->SetQueueEntryNZBName(pNZBInfo, szName);
 
815
}
 
816
 
690
817
/**
691
818
* Check if deletion of already downloaded files is possible (when nzb id deleted from queue).
692
819
* The deletion is most always possible, except the case if all remaining files in queue 
712
839
        return false;
713
840
}
714
841
 
715
 
void QueueEditor::MergeGroups(DownloadQueue* pDownloadQueue, ItemList* pItemList)
 
842
bool QueueEditor::MergeGroups(DownloadQueue* pDownloadQueue, ItemList* pItemList)
716
843
{
717
844
        if (pItemList->size() == 0)
718
845
        {
719
 
                return;
 
846
                return false;
720
847
        }
721
848
 
 
849
        bool bOK = true;
 
850
 
722
851
        EditItem* pDestItem = pItemList->front();
723
852
 
724
853
        for (ItemList::iterator it = pItemList->begin() + 1; it != pItemList->end(); it++)
727
856
                if (pItem->m_pFileInfo->GetNZBInfo() != pDestItem->m_pFileInfo->GetNZBInfo())
728
857
                {
729
858
                        debug("merge %s to %s", pItem->m_pFileInfo->GetNZBInfo()->GetFilename(), pDestItem->m_pFileInfo->GetNZBInfo()->GetFilename());
730
 
                        g_pQueueCoordinator->MergeQueueEntries(pDestItem->m_pFileInfo->GetNZBInfo(), pItem->m_pFileInfo->GetNZBInfo());
 
859
                        if (g_pQueueCoordinator->MergeQueueEntries(pDestItem->m_pFileInfo->GetNZBInfo(), pItem->m_pFileInfo->GetNZBInfo()))
 
860
                        {
 
861
                                bOK = false;
 
862
                        }
731
863
                }
732
864
                delete pItem;
733
865
        }
734
866
 
735
 
        // align group ("AlignGroup" needs the first file item as parameter)
736
 
        for (FileQueue::iterator it = pDownloadQueue->GetFileQueue()->begin(); it != pDownloadQueue->GetFileQueue()->end(); it++)
737
 
        {
738
 
                FileInfo* pFileInfo = *it;
739
 
                if (pFileInfo->GetNZBInfo() == pDestItem->m_pFileInfo->GetNZBInfo())
740
 
                {
741
 
                        AlignGroup(pDownloadQueue, pFileInfo);
742
 
                        break;
743
 
                }
744
 
        }
 
867
        AlignGroup(pDownloadQueue, pDestItem->m_pFileInfo->GetNZBInfo());
745
868
 
746
869
        delete pDestItem;
 
870
        return bOK;
 
871
}
 
872
 
 
873
bool QueueEditor::SplitGroup(DownloadQueue* pDownloadQueue, ItemList* pItemList, const char* szName)
 
874
{
 
875
        if (pItemList->size() == 0)
 
876
        {
 
877
                return false;
 
878
        }
 
879
 
 
880
        FileQueue* pFileList = new FileQueue();
 
881
 
 
882
        for (ItemList::iterator it = pItemList->begin(); it != pItemList->end(); it++)
 
883
        {
 
884
                EditItem* pItem = *it;
 
885
                pFileList->push_back(pItem->m_pFileInfo);
 
886
                delete pItem;
 
887
        }
 
888
 
 
889
        NZBInfo* pNewNZBInfo = NULL;
 
890
        bool bOK = g_pQueueCoordinator->SplitQueueEntries(pFileList, szName, &pNewNZBInfo);
 
891
        if (bOK)
 
892
        {
 
893
                AlignGroup(pDownloadQueue, pNewNZBInfo);
 
894
        }
 
895
 
 
896
        delete pFileList;
 
897
        return bOK;
 
898
}
 
899
 
 
900
void QueueEditor::ReorderFiles(DownloadQueue* pDownloadQueue, ItemList* pItemList)
 
901
{
 
902
        if (pItemList->size() == 0)
 
903
        {
 
904
                return;
 
905
        }
 
906
 
 
907
        EditItem* pFirstItem = pItemList->front();
 
908
        NZBInfo* pNZBInfo = pFirstItem->m_pFileInfo->GetNZBInfo();
 
909
        unsigned int iInsertPos = 0;
 
910
 
 
911
        // find first file of the group
 
912
    for (FileQueue::iterator it = pDownloadQueue->GetFileQueue()->begin(); it != pDownloadQueue->GetFileQueue()->end(); it++)
 
913
    {
 
914
        FileInfo* pFileInfo = *it;
 
915
                if (pFileInfo->GetNZBInfo() == pNZBInfo)
 
916
                {
 
917
                        break;
 
918
                }
 
919
                iInsertPos++;
 
920
        }
 
921
 
 
922
        // now can reorder
 
923
        for (ItemList::iterator it = pItemList->begin(); it != pItemList->end(); it++)
 
924
        {
 
925
                EditItem* pItem = *it;
 
926
                FileInfo* pFileInfo = pItem->m_pFileInfo;
 
927
 
 
928
                // move file item
 
929
                for (FileQueue::iterator it = pDownloadQueue->GetFileQueue()->begin(); it != pDownloadQueue->GetFileQueue()->end(); it++)
 
930
                {
 
931
                        FileInfo* pFileInfo1 = *it;
 
932
                        if (pFileInfo1 == pFileInfo)
 
933
                        {
 
934
                                pDownloadQueue->GetFileQueue()->erase(it);
 
935
                                pDownloadQueue->GetFileQueue()->insert(pDownloadQueue->GetFileQueue()->begin() + iInsertPos, pFileInfo);
 
936
                                iInsertPos++;                           
 
937
                                break;
 
938
                        }
 
939
                }
 
940
 
 
941
                delete pItem;
 
942
        }
747
943
}
748
944
 
749
945
void QueueEditor::SetNZBParameter(NZBInfo* pNZBInfo, const char* szParamString)
761
957
        }
762
958
        else
763
959
        {
764
 
                error("Could not set nzb parameter for %s: invalid argument: %s", Util::BaseFileName(pNZBInfo->GetFilename()), szParamString);
 
960
                error("Could not set nzb parameter for %s: invalid argument: %s", pNZBInfo->GetName(), szParamString);
765
961
        }
766
962
 
767
963
        free(szStr);