Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.09 KB | None | 0 0
  1. /*
  2.  * Copyright 2000-2002, Johan Nilsson. All rights reserved.
  3.  * Copyright 2011 BurnItNow Maintainers
  4.  * Distributed under the terms of the MIT License.
  5.  */
  6.  
  7.  
  8. #include "RightList.h"
  9. #include "AskName.h"
  10. #include "jpWindow.h"
  11.  
  12. #include <Alert.h>
  13. #include <Application.h>
  14. #include <Bitmap.h>
  15. #include <Directory.h>
  16. #include <MenuItem.h>
  17. #include <Path.h>
  18. #include <Button.h>
  19. #include <Resources.h>
  20. #include <TranslationUtils.h>
  21. #include <TranslatorFormats.h>
  22.  
  23. #include <stdio.h>
  24.  
  25.  
  26. bool GlobalPattern = true;
  27.  
  28. extern bool VRCD;
  29. extern char* IMAGE_NAME;
  30. extern char* BURNIT_PATH;
  31. extern char* BURN_DIR;
  32.  
  33.  
  34. BBitmap* GetBitmapResource(type_code type, const char* name)
  35. {
  36.     size_t len = 0;
  37.     BResources* rsrc = BApplication::AppResources();
  38.     const void* data = rsrc->LoadResource(type, name, &len);
  39.  
  40.     if (data == NULL)
  41.         return NULL;
  42.  
  43.     BMemoryIO stream(data, len);
  44.  
  45.     // Try to read as an archived bitmap.
  46.     stream.Seek(0, SEEK_SET);
  47.     BMessage archive;
  48.     status_t err = archive.Unflatten(&stream);
  49.     if (err != B_OK)
  50.         return NULL;
  51.  
  52.     BBitmap* out = new BBitmap(&archive);
  53.     if (!out)
  54.         return NULL;
  55.  
  56.     err = (out)->InitCheck();
  57.     if (err != B_OK) {
  58.         delete out;
  59.         out = NULL;
  60.     }
  61.  
  62.     return out;
  63. }
  64.  
  65.  
  66. FileListItem::FileListItem(entry_ref* ref, char* name, BBitmap* icon)
  67.     :
  68.     BListItem()
  69. {
  70.     if (ref != NULL)
  71.         fRef = *ref;
  72.     fName = new char[strlen(name)+1];
  73.     strcpy(fName, name);
  74.     fIconBitmap = icon;
  75.     fPattern = GlobalPattern;
  76.     GlobalPattern = !GlobalPattern;
  77. }
  78.  
  79.  
  80. FileListItem::~FileListItem()
  81. {
  82.     delete fName;
  83. }
  84.  
  85.  
  86. void FileListItem::DrawItem(BView* owner, BRect frame, bool complete)
  87. {
  88.     rgb_color rgbColor = {52, 73, 154, 255};
  89.     rgb_color rgbSelectedColor = {0, 0, 0, 255};
  90.     rgb_color rgbfPatternColor = {52, 73, 154, 255};
  91.     rgb_color black = {255, 255, 255, 255};
  92.  
  93.     if (IsSelected())
  94.         rgbColor = rgbSelectedColor;
  95.     else if (fPattern)
  96.         rgbColor = rgbfPatternColor;
  97.  
  98.     owner->SetHighColor(rgbColor);
  99.     owner->SetLowColor(rgbColor);
  100.     owner->FillRect(frame);
  101.  
  102.     owner->SetHighColor(black);
  103.  
  104.     if (fIconBitmap != NULL)
  105.         owner->SetDrawingMode(B_OP_ALPHA);
  106.         owner->DrawBitmap(fIconBitmap, BPoint(1, frame.top + 1));
  107.     owner->MovePenTo(BPoint(21, frame.bottom - 1));
  108.     owner->DrawString(fName);
  109. }
  110.  
  111.  
  112. RightList::RightList(BRect size)
  113.     :
  114.     BListView(size, "RightList", B_MULTIPLE_SELECTION_LIST, B_FOLLOW_NONE, B_WILL_DRAW)
  115. {
  116.     SetViewColor(52, 73, 154, 0);
  117.     fBDirectory = new BDirectory(BURN_DIR);
  118.     fTDirectory = new BDirectory(BURN_DIR);
  119.  
  120.     fFileBitmap = BTranslationUtils::GetBitmap('PNG ', "file.png");
  121.     fDirectoryBitmap = BTranslationUtils::GetBitmap('PNG ', "folder.png");
  122.     fInfoBitmap = BTranslationUtils::GetBitmap('PNG ', "info.png");
  123.     UpdateDir();
  124.    
  125.     //BPopUpMenu* fItemPopUpMenu;
  126.     fItemPopUpMenu = new BPopUpMenu("Items Popup");
  127.     fItemPopUpMenu->SetRadioMode(false);
  128.     fItemPopUpMenu->AddItem(new BMenuItem("Move Up", new BMessage('mvup')));
  129.     fItemPopUpMenu->AddItem(new BMenuItem("Move Down", new BMessage('mvdn')));
  130.     fItemPopUpMenu->AddItem(new BMenuItem("Play", new BMessage('play')));
  131.     fItemPopUpMenu->AddItem(new BMenuItem("Remove", new BMessage('reIT')));
  132.     //fItemPopUpMenu->AddItem(new BMenuItem("Delete", new BMessage('deIT')));
  133. }
  134.  
  135.  
  136. RightList::~RightList()
  137. {
  138.     delete fBDirectory;
  139.     delete fTDirectory;
  140.     delete fFileBitmap;
  141.     delete fDirectoryBitmap;
  142.     delete fInfoBitmap;
  143. }
  144.  
  145.  
  146. void RightList::MessageReceived(BMessage* msg)
  147. {
  148.     entry_ref ref;
  149.     int32 counter = 0;
  150.  
  151.     switch (msg->what) {
  152.         case B_SIMPLE_DATA:
  153.             if (VRCD) {
  154.                 while (msg->FindRef("refs", counter++, &ref) == B_OK)
  155.                     MakeLink(&ref);
  156.                 UpdateDir();
  157.             } else {
  158.                 BAlert* MyAlert = new BAlert("BurnItNow", "You have to add a Virtual CD Directory to be able to drop files!", "Ok", NULL, NULL, B_WIDTH_AS_USUAL, B_INFO_ALERT);
  159.                 MyAlert->Go();
  160.             }
  161.             break;
  162.         default:
  163.             BListView::MessageReceived(msg);
  164.             break;
  165.     }
  166. }
  167.  
  168.  
  169. void RightList::DeleteDirFromVRCD(entry_ref* ref)
  170. {
  171.     printf("DeleteDirFromVRCD \n");
  172.     BDirectory* temp_dir;
  173.     entry_ref temp_ref;
  174.     temp_dir = new BDirectory(ref);
  175.    
  176.     while (temp_dir->GetNextRef(&temp_ref) != B_ENTRY_NOT_FOUND)
  177.     {
  178.         if (BEntry(&temp_ref, true).IsDirectory())
  179.         {
  180.             DeleteDirFromVRCD(&temp_ref);
  181.             BEntry(&temp_ref).Remove();
  182.         }
  183.         else
  184.         {
  185.             BEntry(&temp_ref).Remove();
  186.         }
  187.     }
  188.     delete temp_dir;
  189.     printf("DeleteDirFromVRCD END\n");
  190. }
  191.  
  192.  
  193. void RightList::DeleteFromVRCD(entry_ref* ref)
  194. {
  195.     printf("DeleteFromVRCD \n");
  196.     BEntry temp_entry(ref);
  197.    
  198.     char buf[B_PATH_NAME_LENGTH];   temp_entry.GetName( buf);
  199.     WriteLog("Delete:");  WriteLog(  buf );  
  200.  
  201.     if (temp_entry.IsDirectory())
  202.     {   DeleteDirFromVRCD(ref);
  203.         temp_entry.Remove();
  204.     }
  205.     else
  206.     {
  207.         temp_entry.Remove();
  208.     }  
  209.     printf("DeleteFromVRCD END\n");
  210. }
  211.  
  212.  
  213. void RightList::KeyDown(const char* bytes, int32 numBytes)
  214. {
  215.     BPath temp_path;
  216.     int32 result;
  217.     switch (bytes[0]) {
  218.         case B_DELETE: {
  219.                 int32 selection = CurrentSelection();
  220.                 if (selection >= 0) {
  221.                     FileListItem* item = (FileListItem*)ItemAt(selection);
  222.                     BAlert* MyAlert = new BAlert("BurnItNow", "Are you sure you want to delete this selection", "Yes", "No", NULL, B_WIDTH_AS_USUAL, B_INFO_ALERT);
  223.                     MyAlert->SetFeel(B_MODAL_ALL_WINDOW_FEEL);
  224.                     result = MyAlert->Go();
  225.                     if (result == 0)
  226.                     {
  227.                         DeleteFromVRCD(&item->fRef);
  228.                         UpdateDir();
  229.                        
  230.                     } else {
  231.                         //UpdateDir();
  232.                         WriteLog("Didnt delete file");
  233.                     }
  234.  
  235.                     //if (item != NULL) {       delete item; }
  236.  
  237.                 }
  238.                 break;
  239.             }
  240.         default:
  241.         BListView::KeyDown(bytes, numBytes);
  242.     }
  243. }
  244.  
  245.  
  246. void RightList::MouseDown(BPoint point)
  247. {
  248.     int32 result;
  249.     BMessage* msg = Window()->CurrentMessage();
  250.     uint32 clicks = msg->FindInt32("clicks");
  251.     uint32 button = msg->FindInt32("buttons");
  252.     if ((button == fLastButton) && (clicks > 1))
  253.         fClickCount++;
  254.     else
  255.         fClickCount = 1;
  256.  
  257.     fLastButton = button;
  258.    
  259.     if ((button == B_SECONDARY_MOUSE_BUTTON) )
  260.     {
  261.         int32 itemn = IndexOf(point);
  262.         if (itemn >= 0)
  263.         {
  264.             BMenuItem* selected;
  265.             BPoint p = point;
  266.             ConvertToScreen(&p);
  267.             Select(itemn);
  268.             selected = fItemPopUpMenu->Go(p);
  269.             if (selected) {
  270.                 if (!strcmp(selected->Label(), "Remove")) {
  271.                     int32 selection = CurrentSelection();
  272.                     if (selection >= 0) {  
  273.                         FileListItem* item = (FileListItem*)ItemAt(selection);
  274.                         if (item->fIconBitmap != NULL && item->fIconBitmap != fInfoBitmap) {
  275.                             //printf("Will remove %s\n",(char*) item->fName);
  276.                             BAlert* MyAlert = new BAlert("BurnItNow", "Are you sure you want to delete this selection", "Yes", "No", NULL, B_WIDTH_AS_USUAL, B_INFO_ALERT);
  277.                             MyAlert->SetFeel(B_MODAL_ALL_WINDOW_FEEL);
  278.                             result = MyAlert->Go();
  279.                             if (result == 0) {
  280.                                 DeleteFromVRCD(&item->fRef);
  281.                                 UpdateDir();
  282.                             }
  283.                         }
  284.                     }
  285.                 }
  286.             }      
  287.         }
  288.     }
  289.    
  290.    
  291.     if ((button == B_PRIMARY_MOUSE_BUTTON) && (fClickCount == 2)) {
  292.         int32 selection = CurrentSelection();
  293.         if (selection >= 0) {
  294.             FileListItem* item = (FileListItem*)ItemAt(selection);
  295.             if (item->fIconBitmap != NULL && item->fIconBitmap != fInfoBitmap)
  296.                 if (fBDirectory->SetTo(&item->fRef) == B_OK)
  297.                     UpdateDir();
  298.         }
  299.         fClickCount = 0;
  300.     } else
  301.     BListView::MouseDown(point);
  302. }
  303.  
  304.  
  305. void RightList::UpdateInfo(char* str1 = NULL, char* str2 = NULL, char* str3 = NULL, char* str4 = NULL, char* str5 = NULL)
  306. {
  307.     MakeEmpty();
  308.     if (str1 != NULL)
  309.         AddItem(new FileListItem(NULL, str1, fInfoBitmap));
  310.     if (str2 != NULL)
  311.         AddItem(new FileListItem(NULL, str2, NULL));
  312.     if (str3 != NULL)
  313.         AddItem(new FileListItem(NULL, str3, NULL));
  314.     if (str4 != NULL)
  315.         AddItem(new FileListItem(NULL, str4, NULL));
  316.     if (str5 != NULL)
  317.         AddItem(new FileListItem(NULL, str5, NULL));
  318. }
  319.  
  320.  
  321. void RightList::UpdateDir()
  322. {  
  323.     BEntry temp_entry;
  324.     entry_ref temp_ref;    
  325.     status_t ret=B_OK;  
  326.  
  327.     MakeEmpty();
  328.    
  329.     fBDirectory->GetEntry(&temp_entry);
  330.     fBDirectory->SetTo(&temp_entry);
  331.  
  332.     if(fBDirectory->GetEntry(&temp_entry) == B_OK) {
  333.         ret=fBDirectory->SetTo(&temp_entry);       
  334.         if(ret != B_OK) {
  335.             printf(" cant  fBDirectory->SetTo(&temp_entry)\n");
  336.         }  
  337.     }
  338.     else {
  339.         printf(" cant fBDirectory->GetEntry(&temp_entry)\n");
  340.     }
  341.  
  342.     while (fBDirectory->GetNextRef(&temp_ref) != B_ENTRY_NOT_FOUND) {
  343.         if (BEntry(&temp_ref, true).IsDirectory()) {
  344.             AddItem(new FileListItem(&temp_ref, temp_ref.name, fDirectoryBitmap));
  345.         }
  346.         else {
  347.             AddItem(new FileListItem(&temp_ref, temp_ref.name, fFileBitmap));
  348.         }
  349.     }
  350.    
  351.     //Added:
  352.     char buf[B_FILE_NAME_LENGTH];  
  353.     bool enableBut=true;
  354.     temp_entry.GetName(buf);   
  355.     //printf(" ** strstr:  %p | %s\n", strstr("VRCD", buf), buf);
  356.     if( strstr("VRCD", buf) != NULL ) {
  357.         enableBut=false;
  358.     }
  359.  
  360.     jpWindow* win = dynamic_cast<jpWindow*>(Window());  //printf(" *jpWindow %p\n",win);
  361.     if (win != NULL) {
  362.         win->Lock();
  363.         win->fParentDirButton->SetEnabled(enableBut);
  364.         win->Unlock();
  365.     }
  366. }
  367.  
  368.  
  369. void RightList::ParentDir()
  370. {  
  371.     BEntry temp_entry, temp_entry2;  
  372.     BPath temp_path;
  373.    
  374.     if (fBDirectory->GetEntry(&temp_entry) == B_OK) {
  375.         if( temp_entry.GetParent(&temp_entry2) == B_OK) {
  376.             if (temp_entry2.GetPath(&temp_path) == B_OK) {
  377.                 if (!strncmp(temp_path.Path(), BURN_DIR, strlen(BURN_DIR))) {
  378.                     temp_entry.GetParent(fBDirectory);
  379.                     UpdateDir();
  380.                 }
  381.             }
  382.         }
  383.     }
  384. }
  385.  
  386.  
  387. void RightList::CreateDir()
  388. {
  389.     AskName* makeDirWindow = new AskName(BRect(200, 200, 500, 250), "Make directory", MAKE_DIRECTORY, "");
  390.     makeDirWindow->Show();
  391. }
  392.  
  393.  
  394. void RightList::MakeDir(const char* name)
  395. {
  396.     char temp_char[2048];
  397.     BEntry temp_entry;
  398.     BPath temp_path;
  399.     fBDirectory->GetEntry(&temp_entry);
  400.     temp_entry.GetPath(&temp_path);
  401.     sprintf(temp_char, "%s/%s", temp_path.Path(), name);
  402.     if (!BEntry(temp_char).Exists()) {
  403.         fBDirectory->CreateDirectory(temp_char, NULL);
  404.         UpdateDir();
  405.     } else {
  406.         BAlert* MyAlert = new BAlert("BurnItNow", "The directory is already exists!", "Ok", NULL, NULL, B_WIDTH_AS_USUAL, B_INFO_ALERT);
  407.         MyAlert->SetFeel(B_MODAL_ALL_WINDOW_FEEL);
  408.         MyAlert->Go();
  409.     }
  410.  
  411. }
  412.  
  413.  
  414. void RightList::MakeDir(entry_ref* ref)
  415. {
  416.     entry_ref temp_ref;
  417.     char temp_char[4096];
  418.     BPath temp_path;
  419.     BDirectory* temp_dir;
  420.     BEntry temp_entry;
  421.     temp_dir = new BDirectory(ref);
  422.     while (temp_dir->GetNextRef(&temp_ref) == B_OK) {
  423.         if (BEntry(&temp_ref, false).IsDirectory()) {
  424.             fTDirectory->GetEntry(&temp_entry);
  425.             temp_entry.GetPath(&temp_path);
  426.             memset(temp_char, 0, sizeof(temp_char));
  427.             sprintf(temp_char, "%s/%s", temp_path.Path(), temp_ref.name);
  428.             if (fTDirectory->CreateDirectory(temp_char, NULL) == B_OK)
  429.                 if (fTDirectory->SetTo(temp_char) == B_OK) {
  430.                     MakeDir(&temp_ref);
  431.                     fTDirectory->SetTo(temp_path.Path());
  432.                 }
  433.         } else {
  434.             if (!BEntry(&temp_ref, true).IsDirectory()) {
  435.                 if (fTDirectory->GetEntry(&temp_entry) == B_OK)
  436.                     if (temp_entry.GetPath(&temp_path) == B_OK) {
  437.                         sprintf(temp_char, "%s/%s", temp_path.Path(), temp_ref.name);
  438.                         if (BEntry(&temp_ref, false).GetPath(&temp_path) == B_OK)
  439.                             fTDirectory->CreateSymLink(temp_char, temp_path.Path(), NULL);
  440.                     }
  441.             } else {
  442.                 // Don't follow linked directorys
  443.                 WriteLog("RightList::MakeDir -> #1");
  444.             }
  445.         }
  446.     }
  447.     delete temp_dir;
  448. }
  449.  
  450.  
  451. void RightList::MakeLink(entry_ref* ref)
  452. {
  453.     entry_ref temp_ref;
  454.     char temp_char[4096];
  455.     BPath temp_path;
  456.     BEntry temp_entry;
  457.  
  458.     if (BEntry(ref, false).IsDirectory()) {
  459.         fBDirectory->GetEntry(&temp_entry);
  460.         temp_entry.GetPath(&temp_path);
  461.         sprintf(temp_char, "%s/%s", temp_path.Path(), ref->name);
  462.         if (fTDirectory->CreateDirectory(temp_char, NULL) == B_OK)
  463.             if (fTDirectory->SetTo(temp_char) == B_OK) {
  464.                 MakeDir(ref);
  465.                 fTDirectory->Rewind();
  466.             }
  467.     } else {
  468.         if (!BEntry(ref, true).IsDirectory()) {
  469.             if (fBDirectory->GetEntry(&temp_entry) == B_OK) {
  470.                 if (temp_entry.GetPath(&temp_path) == B_OK) {
  471.                     sprintf(temp_char, "%s/%s", temp_path.Path(), ref->name);
  472.                     if (BEntry(ref, false).GetPath(&temp_path) == B_OK)
  473.                         fBDirectory->CreateSymLink(temp_char, temp_path.Path(), NULL);
  474.                 }
  475.             }
  476.         } else {
  477.             // Don't follow linked directorys
  478.             WriteLog("RightList::MakeLink -> #1");
  479.         }
  480.     }
  481.  
  482. }
  483.  
  484.  
  485. void RightList::WriteLog(const char* string)
  486. {  
  487.     jpWindow* win = dynamic_cast<jpWindow*>(Window()); 
  488.     if (win != NULL) {
  489.         win->MessageLog(string);
  490.     }
  491.            
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement