Example #1
0
File: CDoc.cpp Project: diversys/pe
void CDoc::DoSaveACopy(entry_ref& directory, const char *name)
{
	try
	{
		BDirectory dir(&directory);
		BEntry e(&dir, name);
		FailOSErr(e.InitCheck());
		if (e.Exists())
			e.Remove();

		entry_ref eref;
		FailOSErr(e.GetRef(&eref));
		CLocalDocIO writer(this, &eref, NULL);
		writer.WriteDoc();

		if (fSavePanel)
		{
			delete fSavePanel;
			fSavePanel = NULL;
		}
	}
	catch (HErr& e)
	{
		e.DoError();
	}
}
Example #2
0
File: CDoc.cpp Project: diversys/pe
CDoc::CDoc(const char* mimetype, BLooper *target, const entry_ref *doc)
	: fDocIO(NULL)
	, fSavePanel(NULL)
	, fMimeType(mimetype ? mimetype : "")
	, fDirty(false)
	, fReadOnly(false)
	, fEncoding(B_UNICODE_UTF8)
	, fLineEndType(kle_LF)
{
	fDocIO = new CLocalDocIO(this, doc, target);
	FailNil(fDocIO);
	if (doc)
	{
		BEntry e;
		FailOSErr(e.SetTo(doc, true));
		FailOSErr(e.GetParent(&gCWD));

		BNode node;
		FailOSErr(node.SetTo(doc));

		struct stat st;
		FailOSErr(node.GetStat(&st));

		fReadOnly = !((gUid == st.st_uid && (S_IWUSR & st.st_mode))
						||	(gGid == st.st_gid && (S_IWGRP & st.st_mode))
						||	(S_IWOTH & st.st_mode));

		char s[NAME_MAX];
		if (BNodeInfo(&node).GetType(s) == B_OK)
			fMimeType = s;
	}
	sfDocList.push_back(this);
}
Example #3
0
PDoc* PApp::OpenWorksheet()
{
	try
	{
		if (!gPrefsDir.Contains("Worksheet", B_FILE_NODE | B_SYMLINK_NODE))
		{
			BFile file;
			gPrefsDir.CreateFile("Worksheet", &file);
		}

		BEntry w;
		entry_ref wr;

		FailOSErr(gPrefsDir.FindEntry("Worksheet", &w, true));
		FailOSErr(w.GetRef(&wr));
		OpenWindow(wr);

		PDoc *d = dynamic_cast<PDoc*>(CDoc::FindDoc(wr));
		if (d)
			d->MakeWorksheet();

		return d;
	}
	catch (HErr& e)
	{
		e.DoError();
	}

	return NULL;
} /* PApp::OpenWorksheet */
Example #4
0
File: CDoc.cpp Project: diversys/pe
void CDoc::SaveRequested(entry_ref& directory, const char *name)
{
	try
	{
		BDirectory dir(&directory);
		gCWD = dir;

		BEntry e(&dir, name);
		FailOSErr(e.InitCheck());
		if (e.Exists())
		{
			entry_ref xr;

			e.GetRef(&xr);

			if (EntryRef() && xr == *EntryRef())	// its me, help!!!
			{
				BFile file;
				FailOSErr(file.SetTo(EntryRef(), B_READ_WRITE));
				FailOSErr(file.SetSize(0));
			}
			else
				e.Remove();
		}

		fReadOnly = false;

		entry_ref eref;
		FailOSErr(e.GetRef(&eref));
		fDocIO->SetEntryRef(&eref);

		if (!IsDirty())
			SetDirty(true);

		Save();
		NameChanged();

		if (EntryRef())
		{
			BPath p;
			FailOSErr(e.GetPath(&p));
			AddRecent(p.Path());
		}

		if (fSavePanel)
		{
			delete fSavePanel;
			fSavePanel = NULL;
		}
	}
	catch (HErr& e)
	{
		e.DoError();
	}
}
Example #5
0
File: CDoc.cpp Project: diversys/pe
void CDoc::SetMimeType(const char *type, bool updateOnDisk)
{
	fMimeType = type;

	if (updateOnDisk && EntryRef())
	{
		BNode node;
		FailOSErr(node.SetTo(EntryRef()));
		FailOSErr(BNodeInfo(&node).SetType(type));
	}
}
Example #6
0
void PProjectWindow::NameChanged()
{
	inherited::NameChanged();
	if (EntryRef())
	{
		BEntry e;
		BPath p;
		FailOSErr(e.SetTo(EntryRef()));
		FailOSErr(e.GetPath(&p));
		fStatus->SetPath(p.Path());
		AddRecent(p.Path());
	}
}
Example #7
0
File: CDoc.cpp Project: diversys/pe
void CDoc::PostToAll(unsigned long msg, bool async)
{
	doclist::iterator di;

	doclist lst = sfDocList;

	BLooper *me = BLooper::LooperForThread(find_thread(NULL));

	for (di = lst.begin(); di != lst.end(); di++)
	{
		BWindow *w = dynamic_cast<BWindow*>(*di);
		if (w)
		{
			BMessage reply;

			if (async || w == dynamic_cast<BWindow*>(me))
				w->PostMessage(msg);
			else
			{
				BMessenger msgr(w);
				FailOSErr(msgr.SendMessage(msg, &reply));
			}
		}
	}
}
Example #8
0
void CPrefsDialog::GetDefPageSetup()
{
	BPrintJob prJob("a page setup job");
	
	if (fPageSetup)
	{
		BMessage *s = new BMessage;
		if (s && s->Unflatten(fPageSetup) == B_NO_ERROR)
			prJob.SetSettings(s);
	}
	
	int result = prJob.ConfigPage();
	
	if (result == B_NO_ERROR)
	{
		BMessage * s(prJob.Settings());

		fPageSetupSize = s->FlattenedSize();
		if (fPageSetup) FREE(fPageSetup);
		fPageSetup = (char *)MALLOC(fPageSetupSize);
		FailNil(fPageSetup);
		result = s->Flatten(fPageSetup, fPageSetupSize);
		FailOSErr(result, "error flattening (%d)");
		
		UpdateFields();
	}
} /* CPrefsDialog::GetDefPageSetup */
Example #9
0
CMimeItem::CMimeItem(const char *mime)
{
	fMime.SetTo(mime);
	
	memset(fIcon, B_TRANSPARENT_8_BIT, 256);
	memset(fIconSelected, B_TRANSPARENT_8_BIT, 256);

	BBitmap bm(BRect(0, 0, 15, 15), B_COLOR_8_BIT);
	if (fMime.GetIcon(&bm, B_MINI_ICON) != B_OK)
	{
		try
		{
			char p[PATH_MAX];
			if (find_directory(B_SYSTEM_TEMP_DIRECTORY, 0, true, p, PATH_MAX) == B_OK)
			{
				BDirectory tmpdir;
				FailOSErr(tmpdir.SetTo(p));

				time_t t;
				time(&t);
				sprintf(p, "tmp.pe_is_looking_for_a_mime_icon:%ld", t);

				BFile f;
				FailOSErr(tmpdir.CreateFile(p, &f));

				BNodeInfo ni;
				FailOSErr(ni.SetTo(&f));
				FailOSErr(ni.SetType(mime));
				FailOSErr(ni.GetTrackerIcon(&bm, B_MINI_ICON));
			}
		}
		catch (HErr& e) { }
	}

	for (int i = 0; i < 16; i++)
	{
		unsigned char *ba = (unsigned char *)((unsigned char *)bm.Bits() + bm.BytesPerRow() * i);
		memcpy(fIcon + i * 16, ba, 16);
		
		for (int j = 0; j < 16; j++)
			if (ba[j] < 255)
				fIconSelected[j + i * 16] = gSelectedMap[ba[j]];
	}
} /* CMimeItem::CMimeItem */
Example #10
0
void PProjectWindow::AddFiles()
{
	if (fPanel)
		fPanel->Show();
	else
	{
		entry_ref ref;

		if (EntryRef())
		{
			BEntry e, p;
			FailOSErr(e.SetTo(EntryRef()));
			FailOSErr(e.GetParent(&p));
			FailOSErr(p.GetRef(&ref));
		}

		fPanel = new BFilePanel(B_OPEN_PANEL, new BMessenger(this), &ref);
		fPanel->SetButtonLabel(B_DEFAULT_BUTTON, "Add");
		fPanel->Show();
	}
} /* PProjectWindow::AddFiles */
Example #11
0
void PProjectWindow::OpenItem()
{
	PEntryItem *gi;
	gi = dynamic_cast<PEntryItem*>(fList->ItemAt(fList->CurrentSelection()));
	if (gi)
	{
		try
		{
			BNode node;
			FailOSErr(node.SetTo(&gi->Ref()));
			BNodeInfo info;
			FailOSErr(info.SetTo(&node));

			char mime[B_MIME_TYPE_LENGTH];

			CProjectFile* subProject
				= dynamic_cast<CProjectFile*>(gi->ModelItem());
			if (subProject) {
				if (!subProject->HasBeenParsed()) {
					subProject->Read();
					if (subProject->HasBeenParsed()) {
						list<CProjectItem*>::const_iterator iter;
						for( iter = subProject->begin();
							  iter != subProject->end(); ++iter) {
							AddItemsToList( *iter, gi);
						}
					}
				}
			} else if (info.GetType(mime) || strncmp(mime, "text/", 5))
				OpenInTracker(gi->Ref());
			else
				gApp->OpenWindow(gi->Ref());
		}
		catch (HErr& e)
		{
			e.DoError();
			gApp->OpenWindow(gi->Ref());
		}
	}
} /* PProjectWindow::OpenItem */
Example #12
0
HPreferences::HPreferences(const char *preffilename)
{
	BPath settings;

	FailOSErr(find_directory(B_USER_SETTINGS_DIRECTORY, &settings, true));

	char p[PATH_MAX];
	strcpy(p, settings.Path());
	strcat(p, "/");
	strcat(p, preffilename);
	fFile = strdup(p);
	FailNil(fFile);
} /* HPreferences::HPreferences */
Example #13
0
File: CDoc.cpp Project: diversys/pe
void CDoc::SetReadOnly(bool readOnly)
{
	if ((readOnly && fDirty) || !EntryRef())
	{
		MWarningAlert a("You have to save the file first before marking it read-only");
		a.Go();
	}
	else
	{
		try
		{
			BEntry e;
			FailOSErr(e.SetTo(EntryRef(), true));
			BPath p;
			FailOSErr(e.GetPath(&p));

			struct stat st;
			FailOSErr(stat(p.Path(), &st));

			if (readOnly)
				st.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
			else
			{
// is this the right way, or is it a hack???
				mode_t t = umask(0);	// get the umask
				umask(t);				// and reset it.
				st.st_mode |= (S_IWUSR | S_IWGRP | S_IWOTH) & ~t;
			}

			FailOSErr(chmod(p.Path(), st.st_mode));

			fReadOnly = readOnly;
		}
		catch (HErr& e)
		{
			e.DoError();
		}
	}
}
Example #14
0
void CPathsBox::MessageReceived(BMessage *msg)
{
	try
	{
		entry_ref ref;
		BEntry e;
		BPath p;
		
		if (msg->WasDropped() && msg->HasRef("refs"))
		{
			FailOSErr(msg->FindRef("refs", &ref));
			FailOSErr(e.SetTo(&ref));
			FailOSErr(e.GetPath(&p));
			
			fList->AddItem(new BStringItem(p.Path()));
			
			ListBoxChanged();
		}
		else switch (msg->what)
		{
			case msg_AddPath:
			case msg_ChangePath:
				FailOSErr(msg->FindRef("refs", &ref));
				FailOSErr(e.SetTo(&ref));
				FailOSErr(e.GetPath(&p));
				
				if (msg->what == msg_AddPath)
					fList->AddItem(new BStringItem(p.Path()));
				else
				{
					BStringItem *item;
					FailOSErr(msg->FindPointer("item", (void**)&item));
					if (fList->IndexOf(item) < 0)
						THROW(("item was removed from list!"));
					item->SetText(p.Path());
				}
				
				delete fPanel;
				fPanel = NULL;
				
				ListBoxChanged();
				break;
				
			default:
				CListBox::MessageReceived(msg);
				break;
		}
	}
	catch (HErr& e)
	{
		e.DoError();
	}
} /* CPathsBox::MessageReceived */
Example #15
0
void PProjectWindow::WriteAttr(BFile& file, const BMessage& settingsMsg)
{
	char *fm = NULL;
	try
	{
		ssize_t s = settingsMsg.FlattenedSize();
		fm = (char *)malloc(s);
		FailNil(fm);
		FailOSErr(settingsMsg.Flatten(fm, s));
		FailIOErr(file.WriteAttr("pe-prj-info", 'info', 0, fm, s));
	}
	catch (HErr& e) {}
	if (fm)
		free(fm);
}
Example #16
0
File: CDoc.cpp Project: diversys/pe
void CDoc::AddRecent(const char *path)
{
	char *s;
	if ((s = strstr(path, "/config/settings/pe/Worksheet")) != NULL &&
		strlen(s) == 29 /*strlen("/config/settings/pe/Worksheet")*/)
		return; // don't add the worksheet

	if (gPrefs->GetPrefInt(prf_I_SkipTmpFiles, 1))
	{
		BPath tp;
		try
		{
			FailOSErr(tp.SetTo("/tmp", NULL, true));
			if (strncmp(tp.Path(), path, strlen(tp.Path())) == 0)
				return;
		}
		catch (...) {}
	}

	vector<char*>::iterator di;

	for (di = sfTenLastDocs.begin(); di != sfTenLastDocs.end(); di++)
	{
		char *p = *di;
		if (p && strcmp(p, path) == 0)
		{
			if (di != sfTenLastDocs.begin())
			{
				sfTenLastDocs.erase(di);
				sfTenLastDocs.insert(sfTenLastDocs.begin(), p);
			}
			return;
		}
	}

	while (sfTenLastDocs.size() >= gRecentBufferSize)
	{
		char *p = sfTenLastDocs.back();
		sfTenLastDocs.erase(sfTenLastDocs.end() - 1);
		free(p);
	}

	sfTenLastDocs.insert(sfTenLastDocs.begin(), strdup(path));
}
Example #17
0
void PProjectWindow::ReadAttr(BFile& file, BMessage& settingsMsg)
{
	char *fm = NULL;
	try
	{
		attr_info ai;
		if (file.GetAttrInfo("pe-prj-info", &ai) == B_NO_ERROR)
		{
			fm = (char *)malloc(ai.size);
			FailNil(fm);

			FailIOErr(file.ReadAttr("pe-prj-info", ai.type, 0, fm, ai.size));

			FailOSErr(settingsMsg.Unflatten(fm));
		}
	}
	catch (HErr& e) {}
	if (fm)
		free(fm);
}
Example #18
0
BHandler *PApp::ResolveSpecifier(BMessage *msg, int32 index,
			BMessage *specifier, int32 form, const char *property)
{
	BHandler *result = NULL;

	try
	{
		PDoc *doc = NULL;

		if (strcmp(property, "Window") == 0 && form == B_NAME_SPECIFIER)
		{
			msg->PopSpecifier();

			entry_ref file;
			const char *fileName;

			FailOSErr(specifier->FindString("name", &fileName));
			if (get_ref_for_path(fileName, &file) == B_OK)
			{
				doc = dynamic_cast<PDoc*>(CDoc::FindDoc(file));
				if (doc == NULL)
					doc = dynamic_cast<PDoc*>(OpenWindow(file, true));
			}
		}

		if (doc)
			doc->PostMessage(msg);
		else
			result = BApplication::ResolveSpecifier(msg, index, specifier, form, property);
	}
	catch (HErr& e)
	{
		BMessage reply;
		reply.AddString("Error", e);
		msg->SendReply(&reply);
	}

	return result;
} /* PApp::ResolveSpecifier */
Example #19
0
void CFtpDialog::ListDirectory()
{
	if (fSocket < 0)
	{
		beep();
		return;
	}

	for (int i = fListView->CountItems() - 1; i >= 0; i--)
		delete fListView->RemoveItem(i);

	int data = 0;

	try
	{
		struct sockaddr_in saData;
		struct sockaddr_in saCmd;

		data = socket(AF_INET, SOCK_STREAM, 0);
		if (data < 0)
			THROW(("Failed to get socket: %s", strerror(errno)));

		memset(&saData, 0, sizeof(saData));
		saData.sin_family = AF_INET;
		socklen_t size = sizeof(saData);

		bool passive = IsOn("pssv");
		if (passive) {
			// switch to passive mode
			s_printf(fSocketFD, "pasv\r\n");
		} else {
			FailSockErr(bind(data, (struct sockaddr *)&saData, sizeof(saData)));
			FailSockErr(listen(data, 5));
			// [zooey]: calling getsockname() on a socket that has been bound to
			// IN_ADDR_ANY (the wildcard-address) will *not* return any IP-address,
			// as this will only be setup by the system during connect or accept.
			// 	[refer to W.R. Stevens - Unix Network Programming, Vol 1, p. 92]
			// BeOS R5 however, *does* fill in the IP-address at this stage (that's
			// why this code worked for R5 but didn't work for BONE).
			// In order to fix this problem, we simply use the IP-address of the
			// command-socket for the PORT-command:
			//
			// fetch port from data-socket:
			FailSockErr(getsockname(data, (struct sockaddr *)&saData, &size));
			unsigned char *pap = (unsigned char *)&saData.sin_port;
			// fetch ip-address from cmd-socket:
			FailSockErr(getsockname(fSocketFD->sSocket, (struct sockaddr *)&saCmd, 
											&size));
			unsigned char *sap = (unsigned char *)&saCmd.sin_addr.s_addr;
			// combine both into the PORT-command:
			s_printf(fSocketFD, "port %d,%d,%d,%d,%d,%d\r\n", sap[0], sap[1], 
						sap[2], sap[3], pap[0], pap[1]);
		}

		int state = 1;
		SOCK* dsf = NULL;

		while (state)
		{
			GetReply();

			switch (state)
			{
				case 1:
					if (passive) {
						unsigned int sap[4];
						unsigned int pap[2];
						if (*fReply != '2')
							THROW(("Pasv command failed: %s", fReply));
						char* pos = strchr(fReply,'(');
						if (!pos)
							THROW(("Answer to Pasv has unknown format: %s", fReply));
						int cnt = sscanf(pos+1, "%u,%u,%u,%u,%u,%u", 
											  &sap[0], &sap[1], &sap[2], &sap[3], 
											  &pap[0], &pap[1]);
						if (cnt != 6)
							THROW(("Could not parse answer to Pasv (%d of 6): %s", 
									 cnt, fReply));
						char ipAddr[20];
						sprintf(ipAddr, "%d.%d.%d.%d", sap[0], sap[1], sap[2], sap[3]);
						saData.sin_port = htons(pap[0]*256+pap[1]);
						saData.sin_addr.s_addr = inet_addr(ipAddr);
						FailOSErr(connect(data, (struct sockaddr *)&saData, sizeof(saData)));
						dsf = s_open(data, "r+");
					} else {
						if (*fReply != '2')
							THROW(("Port command failed: %s", fReply));
					}
					s_printf(fSocketFD, "list\r\n");
					state = 2;
					break;

				case 2:
					if (*fReply == '1')
					{
						int ds = 0;
						if (!passive) {
							FailSockErr(ds = accept(data, (struct sockaddr *)&saData, &size));
							dsf = s_open(ds, "r+");
						}

						try
						{
							CFtpListItem *item;
							char s[256];
							bool showAll = IsOn("dotf");
							int  entryCount = 0;

							while (s_gets(s, 256, dsf))
							{
								entryCount++;
								item = new CFtpListItem(this, s);
								if (item->IsValid() && (showAll || !item->IsDotFile()))
								{
									fListView->AddItem(item);
								}
								else
									delete item;
							}
							if (entryCount == 0)
								THROW(("Could not get listing."));

							fListView->Invalidate();
							UpdateIfNeeded();
							s_close(dsf);
							if (!passive)
								closesocket(ds);
						}
						catch (HErr& e)
						{
							EnableUpdates();
							s_close(dsf);
							closesocket(ds);
							throw;
						}

						state = 3;
					}
					else
						THROW(("Failed to get listing: %s", fReply));
					break;

				case 3:
					if (*fReply != '2')
						THROW(("Something went wrong fetching the directory listing"));
					state = 0;
					break;
			}
		}

		closesocket(data);
	}
	catch (HErr& e)
	{
		if (data) closesocket(data);
		e.DoError();
	}
} // CFtpDialog::ListDirectory
Example #20
0
void CFtpDialog::MessageReceived(BMessage *msg)
{
	switch (msg->what)
	{
		case 'cnct':
			Connect();
			break;

		case msg_ServerNameChanged:
			fServerName->MarkAsInvalid(false);
			break;

		case msg_SelectedDirectory:
		{
			BMenuItem *src;
			FailOSErr(msg->FindPointer("source", (void**)&src));

			strcpy(fPath, "/");
			if (src != fDirectoryField->Menu()->ItemAt(0))
			{
				int i = 1;

				while (true)
				{
					if (i >= fDirectoryField->Menu()->CountItems())
						break;

					BMenuItem *I = fDirectoryField->Menu()->ItemAt(i);

					strcat(fPath, I->Label());
					strcat(fPath, "/");

					if (src == I)
						break;

					++i;
				}
			}

			ChangeDirectory();
			break;
		}

		case msg_SelectedListItem:
		{
			CFtpListItem *i = dynamic_cast<CFtpListItem*>(
				fListView->ItemAt(fListView->CurrentSelection()));
			if (i == reinterpret_cast<CFtpListItem*>(NULL))
			{
				beep();
				return;
			}

			if (i->IsDirectory())
			{
				strcat(fPath, *i);
				strcat(fPath, "/");
				ChangeDirectory();
			}
			else if (OkClicked())
				Quit();
			break;
		}

		case msg_ToggleDot:
			ListDirectory();
			break;

		default:
			HDialog::MessageReceived(msg);
			break;
	}
} // CFtpDialog::MessageReceived
Example #21
0
CBenaphore::CBenaphore(const char *name)
{
	fSemID = create_sem(0, name);
	fCount = 0;
	if (fSemID < 0) FailOSErr(fSemID);
} /* CBenaphore::CBenaphore */
Example #22
0
void CFtpStream::Automaton(int action)
{
	int ctrl = 0, data = 0, r;
	char msg[1024];
	SOCK *csSock = NULL;
	
	try
	{
		if (! fURL.IsValid())
			THROW(("The data to connect is not complete"));
		
		ctrl = socket(AF_INET, SOCK_STREAM, 0);
		if (ctrl < 0)
			THROW(("Failed to get socket: %s", strerror(errno)));
		data = socket(AF_INET, SOCK_STREAM, 0);
		if (data < 0)
			THROW(("Failed to get socket: %s", strerror(errno)));
		
		csSock = s_open(ctrl, "r+");
		
		struct hostent *host;
		if ((host = gethostbyname(fURL.Server())) == NULL)
			THROW(("Failed to get server address: %s", strerror(errno)));
		
		struct sockaddr_in sa;
		sa.sin_family = AF_INET;
		sa.sin_port = htons(21);
		sa.sin_addr.s_addr = *(unsigned int *)host->h_addr;
		
		FailOSErr(connect(ctrl, (struct sockaddr *)&sa, sizeof(sa)));
		
		mail_pop_account pa;
		string username, password;

		username = strlen(fURL.Username()) ? fURL.Username() : "anonymous";

		if (strlen(fURL.Password()))
			password = fURL.Password();
		else if (get_pop_account(&pa) == B_OK)
			password = pa.reply_to;
		else
			password = "******";
		
		struct sockaddr_in saData;
		memset(&saData, 0, sizeof(saData));
		saData.sin_family = AF_INET;

		int state = 1;
		while (state)
		{
			GetReply(csSock, r, msg);

			switch (state)
			{
				case 1:
					if ((r / 100) != 2)
						THROW(("Connect failed: %s", msg));
					s_printf(csSock, "user %s\r\n", username.c_str());
					state = 2;
					break;
				
				case 2:
					if ((r / 100) == 3)
					{
						s_printf(csSock, "pass %s\r\n", password.c_str());
						state = 3;
						break;
					}
					else if ((r / 100) != 2 && (r / 100) != 5)
						THROW(("Failed to login: %s", msg));

					// fall thru
				
				case 3:
					if ((r / 100) == 5)
					{
						bool ok;
						
						CLogin *l = DialogCreator<CLogin>::CreateDialog(NULL);
						FailNil(l);

						l->Connect(fURL.Server(), username, password, &ok);
						
						if (ok)
						{
							s_printf(csSock, "noop\r\n");
							state = 1;
							continue;
						}
						else
							THROW((0));
					}
					else if (state == 3 && (r / 100) != 2)
						THROW(("Failed to login: %s", msg));
					else if (strlen(fURL.Path()))
					{
						s_printf(csSock, "cwd %s\r\n", fURL.Path());
						state = 4;
						break;
					}
		
					// fall thru
				
				case 4:
				{
					if (state == 4 && (r / 100) != 2)
						THROW(("Failed to change directory: %s", msg));
					
					if (fPassive) {
						// switch to passive mode
						s_printf(csSock, "pasv\r\n");
					} else {
						FailSockErr(bind(data, (struct sockaddr *)&saData, sizeof(saData)));
						FailSockErr(listen(data, 5));
						// [zooey]: calling getsockname() on a socket that has been bound to 
						// IN_ADDR_ANY (the wildcard-address) will *not* return any IP-address,
						// as this will only be setup by the system during connect or accept.
						// 		[refer to W.R. Stevens - Unix Network Programming, Vol 1, p. 92]
						// BeOS R5 however, *does* fill in the IP-address at this stage (that's
						// why this code worked for R5 but didn't work for BONE).
						// In order to fix this problem, we simply use the IP-address of the
						// command-socket for the PORT-command:
						socklen_t size = sizeof(saData);
						// fetch port from data-socket:
						FailSockErr(getsockname(data, (struct sockaddr *)&saData, &size));
						unsigned char *pap = (unsigned char *)&saData.sin_port;
						// fetch ip-address from cmd-socket:
						FailSockErr(getsockname(csSock->sSocket, (struct sockaddr *)&sa, &size));
						unsigned char *sap = (unsigned char *)&sa.sin_addr.s_addr;
						s_printf(csSock, "port %d,%d,%d,%d,%d,%d\r\n", sap[0], sap[1], sap[2], sap[3], pap[0], pap[1]);
					}
					state = 5;
					break;
				}
				case 5:
					if (fPassive) {
						unsigned int sap[4];
						unsigned int pap[2];
						if ((r / 100) != 2)
							THROW(("Pasv command failed: %s", msg));
						char* pos = strchr(msg,'(');
						if (!pos)
							THROW(("Answer to Pasv has unknown format: %s", msg));
						int cnt = sscanf(pos+1, "%u,%u,%u,%u,%u,%u", 
											  &sap[0], &sap[1], &sap[2], &sap[3], 
											  &pap[0], &pap[1]);
						if (cnt != 6)
							THROW(("Could not parse answer to Pasv (%d of 6): %s", 
									 cnt, msg));
						char ipAddr[20];
						sprintf(ipAddr, "%d.%d.%d.%d", sap[0], sap[1], sap[2], sap[3]);
						saData.sin_port = htons(pap[0]*256+pap[1]);
						saData.sin_addr.s_addr = inet_addr(ipAddr);
						FailOSErr(connect(data, (struct sockaddr *)&saData, sizeof(saData)));
					} else {
						if ((r / 100) != 2)
							THROW(("Port command failed: %s", msg));
					}
					if (action == 1)
					{
						s_printf(csSock, "retr %s\r\n", fURL.File());
						state = 6;
					}
					else
					{
						s_printf(csSock, "stor %s\r\n", fURL.File());
						state = 7;
					}
					break;
				
				case 6:
					if ((r / 100) == 1)
					{
						int ds;
						socklen_t size = sizeof(sa);
						if (fPassive)
							ds = data;
						else
							FailSockErr(ds = accept(data, (struct sockaddr *)&sa, &size));
						
						try
						{
							do
							{
								FailSockErr(r = recv(ds, msg, 1024, 0));
								if (r)
									fData.Write(msg, r);
							}
							while (r);

							closesocket(ds);
						}
						catch (HErr& e)
						{
							closesocket(ds);
							throw;
						}
						
						state = 8;
					}
					else
						THROW(("Failed to retrieve file: %s", msg));
					break;
					
				case 7:
					if ((r / 100) == 1)
					{
						int ds;
						socklen_t size = sizeof(sa);
						if (fPassive)
							ds = data;
						else
							FailSockErr(ds = accept(data, (struct sockaddr *)&sa, &size));
						
						try
						{
							FailSockErr(send(ds, fData.Buffer(), fData.BufferLength(), 0));
							closesocket(ds);
						}
						catch (HErr& e)
						{
							closesocket(ds);
							throw;
						}
						
						state = 8;
					}
					else
						THROW(("Failed to store file: %s", msg));
					break;

				case 8:
					if ((r / 100) != 2)
						THROW(("Failed to %s file: %s", action == 1 ? "retrieve" : "store",  msg));
					s_printf(csSock, "quit\r\n");
					state = 0;
					break;

			}
		}
		
		s_close(csSock);
		if (!fPassive)
			closesocket(data);
		closesocket(ctrl);
	}
	catch (HErr& e)
	{
		if (csSock) s_close(csSock);
		if (ctrl) closesocket(ctrl);
		if (data) closesocket(data);

		throw;
	}
} /* CFtpStream::Automaton */
Example #23
0
void CCellView::SelectCell(BPoint where)
{
	cell curCell, anchorCell, oldCurCell, tmpCell, prevCurCell;
	BPoint cPoint;
	range r, prevSel;
	BRegion lastSelectionRgn, newSelectionRgn;
	bool updateBorders = (fSelection.top == 1 && fSelection.bottom == kRowCount) ||
						 (fSelection.left == 1 && fSelection.right == kColCount);
	BMessage * current = Window()->CurrentMessage();
	int32 modifiers;
	FailOSErr(current->FindInt32("modifiers", &modifiers), errMessageMissing);

	prevSel = fSelection;
	prevCurCell = fCurCell;

	StClipCells clip(this);

	ClearAnts();

	cPoint = where;
	(void)GetCellHitBy(cPoint, curCell);

	if (!fEntering)
		HiliteSelection(false, false);

	if (!fEntering && modifiers & B_SHIFT_KEY)
	{
		if (curCell.v < fSelection.top)
			anchorCell.v = fSelection.bottom;
		else
			anchorCell.v = fSelection.top;
		if (curCell.h < fSelection.left)
			anchorCell.h = fSelection.right;
		else
			anchorCell.h = fSelection.left;

		fSelection.Set(std::min(anchorCell.h, curCell.h),
					   std::min(anchorCell.v, curCell.v),
					   std::max(anchorCell.h, curCell.h),
					   std::max(anchorCell.v, curCell.v));
	}
	else
	{
		anchorCell.h = r.right = r.left = curCell.h;
		anchorCell.v = r.bottom = r.top = curCell.v;
			
		fCurCell = curCell;
		fSelection = r;
	}

	if (!fEntering)
		HiliteSelection(false, true);

	DrawStatus();
	if (updateBorders)
		DrawBorders();

	oldCurCell = curCell;

	ulong buttons;
	GetMouse(&cPoint, &buttons);
	while (buttons)
	{
		(void)GetCellHitBy(cPoint, curCell);

		SelectionToRegion(lastSelectionRgn);

		if (curCell.v == anchorCell.v)
			r.bottom = r.top = curCell.v;
		else if (curCell.v < anchorCell.v)
		{
			r.top = curCell.v;
			r.bottom = anchorCell.v;
		}
		else
		{
			r.bottom = curCell.v;
			r.top = anchorCell.v;
		}

		if (curCell.h == anchorCell.h)
			r.right = r.left = curCell.h;
		else if (curCell.h < anchorCell.h)
		{
			r.left = curCell.h;
			r.right = anchorCell.h;
		}
		else
		{
			r.right = curCell.h;
			r.left = anchorCell.h;
		}

		if (fEntering && ((oldCurCell.v != curCell.v) || (oldCurCell.h != curCell.h)))
			ClearAnts();

		fSelection = r;
		SelectionToRegion(newSelectionRgn);

		if (!fEntering)
			ChangeSelection(&lastSelectionRgn, &newSelectionRgn);

		if (((fCurCell.v != fSelection.top) ||
			(fCurCell.h != fSelection.left)) && !fEntering)
			ClearAnts();
		fCurCell = fSelection.TopLeft();

		if (oldCurCell.v != curCell.v || oldCurCell.h != curCell.h)
		{
			DrawStatus();

			tmpCell = fCurCell;
			fCurCell = curCell;
			if (fEntering)
				fSelection = prevSel;
			AdjustScrollBars();
			ScrollToSelection();
			fSelection = r;
			fCurCell = tmpCell;
		}

		MarchAnts();

		oldCurCell = curCell;
		GetMouse(&cPoint, &buttons);
	}

	if (fEntering)
	{
		ClearAnts();
		fEditBox->EnterRange(fSelection);

		fSelection = prevSel;
		fCurCell = prevCurCell;
	}
	else
		fCurCell = fSelection.TopLeft();

	AdjustScrollBars();
	DrawStatus();
}
Example #24
0
void PStatus::MouseDown(BPoint where)
{
	bigtime_t longEnough = system_time() + 250000;
	
	do
	{
		BPoint p;
		unsigned long btns;
		
		GetMouse(&p, &btns);
		
		if (!btns)
		{
			Window()->PostMessage(msg_GoToLine, fText);
			return;
		}
		
		if (fabs(where.x - p.x) > 2 || fabs(where.y - p.y) > 2)
			break;
	}
	while (system_time() < longEnough);

	if (fPath)
	{
		BPopUpMenu popup("no title");
		popup.SetFont(be_plain_font);
		
		char *s = strdup(fPath), *d;
	
		d = strrchr(s, '/');
		if (d) *d = 0;
		
		d = strtok(s, "/");
		while (d)
		{
			popup.AddItem(new BMenuItem(d, NULL), 0);
			d = strtok(NULL, "/");
		}
		
		where.y = Bounds().bottom + 1;
		BMenuItem *i = popup.Go(ConvertToScreen(where), true, false, ConvertToScreen(Bounds()));

		if (i)
		{
			free(s);
			s = strdup(fPath);
			d = strchr(s, '/');
			FailNil(d);
	
			int ix = popup.CountItems() - popup.IndexOf(i);
			
			while (ix--)
				d = strchr(d + 1, '/');
			
			FailNil(d);
			*d = 0;
	
			entry_ref ref;
			FailOSErr(get_ref_for_path(s, &ref));
			OpenInTracker(ref);
		}
		
		free(s);
	}
	else
		beep();
} /* PStatus::MouseDown */
Example #25
0
void PApp::ArgvReceived(int32 argc, char **argv)
{
	try
	{
		int i = 1, lineNr = -1;
		char *p;

		while (i < argc)
		{
			switch (argv[i][0])
			{
				case '-':
					if (strcmp(argv[i], "-reload_worksheet") == 0)
					{
						PDoc *d = OpenWorksheet();
						if (d && d->Lock())
							d->Quit();
						d = OpenWorksheet();
					} else {
						Usage();
					}
					break;

				case '+':
					lineNr = strtoul(argv[i] + 1, &p, 10) - 1;
					if (!p || p == argv[i] + 1) Usage();
					break;

				default:
				{
					BPath path;
					if (argv[i][0] == '/')
						path.SetTo(argv[i]);
					else
						path.SetTo("/boot/home");
					FailOSErr (path.InitCheck());
					entry_ref doc;
					CDocWindow *d;

					FailOSErr (get_ref_for_path(path.Path(), &doc));

					BEntry e;
					FailOSErr(e.SetTo(&doc));

					if (e.Exists())
						d = dynamic_cast<CDocWindow*>(OpenWindow(doc));
					else
					{
						d = NewWindow(NULL);
						d->SetEntryRef(&doc);
					}

					if (d && lineNr >= 0)
					{
						BMessage m(msg_SelectLines);
						m.AddInt32("from", lineNr);
						m.AddInt32("to", lineNr - 1);
						d->PostMessage(&m, d->PreferredHandler());
					}
					break;
				}
			}
			i++;
		}
	}
	catch (HErr& e)
	{
		e.DoError();
	}
} /* PApp::ArgvReceived */
Example #26
0
void PApp::RefsReceived(BMessage	*inMessage)
{
	try
	{
		unsigned long type;
		long count;

		inMessage->GetInfo("refs", &type, &count);

		for (int i = 0; i < count; i++)
		{
			entry_ref doc;
			FailOSErr (inMessage->FindRef("refs", i, &doc));
			PDoc *d = dynamic_cast<PDoc*>(OpenWindow(doc));
			if (!d) continue;
			
			if (inMessage->HasData(kTokenIdentifier, kTokenIDType))
			{
				TokenIdentifier *ident;
				ssize_t size;

				FailOSErr(inMessage->FindData(kTokenIdentifier, kTokenIDType, (const void**)&ident, &size));

				BMessage msg(msg_SelectError);
				FailOSErr(msg.AddInt32("start", ident->eOffset));
				FailOSErr(msg.AddInt32("length", ident->eLength));

				BMessenger msgr(d->TextView());
				FailOSErr(msgr.SendMessage(&msg));
			}
			else if (inMessage->HasInt32("be:selection_offset"))
			{
				int32 offset, length;

				FailOSErr(inMessage->FindInt32("be:selection_offset", &offset));
				FailOSErr(inMessage->FindInt32("be:selection_length", &length));

				BMessage msg(msg_Select);
				FailOSErr(msg.AddInt32("anchor", offset));
				FailOSErr(msg.AddInt32("caret", offset + length));

				BMessenger msgr(d->TextView());
				FailOSErr(msgr.SendMessage(&msg));
			}
			else if (inMessage->HasInt32("be:line"))
			{
				int32 line;

				FailOSErr(inMessage->FindInt32("be:line", &line));

				BMessage msg(msg_SelectLines);
				FailOSErr(msg.AddInt32("from", line));
				FailOSErr(msg.AddInt32("to", line - 1));

				BMessenger msgr(d->TextView());
				FailOSErr(msgr.SendMessage(&msg));
			}
		}
	}
	catch (HErr& e)
	{
		e.DoError();
	}
} /* PApp::RefsReceived */
Example #27
0
void PApp::MessageReceived(BMessage *msg)
{
	try
	{
		switch (msg->what)
		{
			case msg_SaveAll:
			{
				const doclist& lst = CDoc::DocList();
				doclist::const_iterator di;

				for (di = lst.begin(); di != lst.end(); di++)
				{
					BWindow *w = dynamic_cast<PDoc*>(*di);
					if (w)
						w->PostMessage(msg_Save);
				}
				break;
			}

			case msg_CloseAll:
			{
				const doclist& lst = CDoc::DocList();
				doclist::const_reverse_iterator di;

				for (di = lst.rbegin(); di != lst.rend(); di++)
				{
					PDoc *doc = dynamic_cast<PDoc*>(*di);

					if (doc && ! doc->IsWorksheet() && doc->Lock())
					{
						if (doc->QuitRequested())
							doc->Quit();
						else
						{
							doc->Unlock();
							break;
						}
					}
				}
				break;
			}

			case B_NODE_MONITOR:
			{
				CDoc::HandleFolderNodeMonitorMsg(msg);
				break;
			}

			case msg_OpenSelected:
				DialogCreator<COpenSelection>::CreateDialog(NULL);
				break;

			case msg_OpenInclude:
			{
				const char *i;
				FailOSErr(msg->FindString("include", &i));
				const char *src = NULL;
				msg->FindString("from-source", &src);
				FindAndOpen(i, src);
				break;
			}

			case msg_FindCmd:
			{
				int c = 1 << current_workspace();

				if (gPrefs->GetPrefInt(prf_I_SmartWorkspaces, 1))
					FindDialog()->SetWorkspaces(c);

				FindDialog()->SetCaller(PDoc::TopWindow());
				FindDialog()->Show();

				FindDialog()->Activate(true);
				break;
			}

			case B_REFS_RECEIVED:
			case 'OpFi':
				RefsReceived(msg);
				break;

			case msg_About:
			{
				AboutWindow *abwin = new AboutWindow();
				abwin->Show();
				break;
			}
			
			case msg_IdeBringToFront:
				PDoc::IDEBringToFront();
				break;

			case msg_IdeProjectToGroup:
				PDoc::IDEProject2Group();
				break;

			case msg_FindDifferences:
			{
				BRect r(100,100,500,250);
				new CDiffWindow(r, "Differences");
				break;
			}

			case msg_Open:
			{
				if (fOpenPanel->IsShowing())
				{
					fOpenPanel->Window()->SetWorkspaces(1 << current_workspace());
					fOpenPanel->Window()->Activate();
				}
				else
				{
					BEntry entry;
					gCWD.GetEntry(&entry);

					BAutolock lock(fOpenPanel->Window());

					entry_ref ref;
					entry.GetRef(&ref);
					fOpenPanel->SetPanelDirectory(&ref);
					fOpenPanel->Window()->SetWorkspaces(1 << current_workspace());

					if (gPrefs->GetPrefInt(prf_I_ZoomOpenPanel, 0))
					{
						BRect r = BScreen().Frame();

						fOpenPanel->Window()->MoveTo(r.left + 80, r.top + 40);
						fOpenPanel->Window()->ResizeTo(480, r.Height() - 50);
					}

					fOpenPanel->Show();
				}
				break;
			}

			case msg_CommandLineOpen:
			{
				entry_ref doc;
				FailOSErr (msg->FindRef("refs", &doc));

				CDocWindow *w;
				BEntry e;

				if (e.SetTo(&doc) == B_OK && e.Exists())
					w = dynamic_cast<CDocWindow*>(OpenWindow(doc));
				else
				{
					w = NewWindow(NULL);
					w->SetEntryRef(&doc);
				}

				long lineNr;
				if (w && msg->FindInt32("line", &lineNr) == B_OK)
				{
					BMessage m(msg_SelectLines);
					FailOSErr(m.AddInt32("from", lineNr));
					FailOSErr(m.AddInt32("to", lineNr - 1));
					w->PostMessage(&m, w->PreferredHandler());
				}

				if (w)
				{
					BMessage reply;
					reply.AddInt32("thread", w->Thread());
					msg->SendReply(&reply);
				}
				break;
			}

			case B_SILENT_RELAUNCH:
			case msg_New:
				if (be_roster->IsRunning("application/x-vnd.dw-Paladin"))
				{
					BMessage newMsg(PALADIN_SHOW_ADD_NEW_PANEL);
					BMessenger msgr("application/x-vnd.dw-Paladin");
					msgr.SendMessage(&newMsg);
				}
				else
					NewWindow();
				break;

			case msg_Select:
			{
				PDoc *doc;
				if ((doc = PDoc::TopWindow()) != NULL)
				{
					BMessage copy(*msg);
					doc->PostMessage(&copy);
				}
				break;
			}

			case msg_DocClosed:
				if (CDoc::CountDocs() == 0)
					Quit();
				break;

			case msg_Tile:
				PDoc::Tile();
				break;

			case msg_Stack:
				PDoc::Stack();
				break;

			case msg_Zoom:
				if (PDoc::TopWindow())
					PDoc::TopWindow()->Zoom();
				break;

			case msg_Worksheet:
				OpenWorksheet();
				break;

			case msg_NewGroup:
				new PGroupWindow;
				break;

			case 1:
				puts(rcsid);
				break;

			case msg_Quit:
				PostMessage(B_QUIT_REQUESTED);
				break;

			case msg_Preferences:
			{
				long l;
				if (fPrefOpener) wait_for_thread(fPrefOpener, &l);
				fPrefOpener = (thread_id)NULL;
				if (fPrefsDialog) {
					BAutolock lock(fPrefsDialog);
					if (!lock.IsLocked()) {
						THROW(("Preferences panel failed to lock"));
						break;
					}

					int c = 1 << current_workspace();

					if (gPrefs->GetPrefInt(prf_I_SmartWorkspaces, 1))
						fPrefsDialog->SetWorkspaces(c);

					if (fPrefsDialog->IsHidden()) {
						fPrefsDialog->Show();
					}

					fPrefsDialog->Activate(true);
				}
				else
					THROW(("Preferences panel failed to open"));
				break;
			}

			default:
				BApplication::MessageReceived(msg);
		}
	}
	catch (HErr& e)
	{
		e.DoError();
	}
} /* PApp::MessageReceived */
Example #28
0
CInfoDialog::CInfoDialog(BRect frame, const char *name, window_type type, int flags,
			BWindow *owner, BPositionIO* data)
	: HDialog(frame, name, type, flags, owner, data)
{
	fDoc = dynamic_cast<PDoc*>(owner);
	FailNil(fDoc);

	SetText("name", owner->Title());

	char s[32];
	sprintf(s, "%d", fDoc->TextView()->Size());
	SetText("docsize", s);

	sprintf(s, "%d", fDoc->TextView()->LineCount());
	SetText("lines", s);

	BMenuField *mf = dynamic_cast<BMenuField*>(FindView("mime"));
	FailNil(mf);
	fTypes = mf->Menu();

	const char *p;
	int i = 0;
	while ((p = gPrefs->GetIxPrefString(prf_X_Mimetype, i++)) != NULL)
		fTypes->AddItem(new BMenuItem(p, new BMessage(msg_FieldChanged)));

	if (i == 1)
		fTypes->AddItem(new BMenuItem("text/plain", new BMessage(msg_FieldChanged)));

	const char *mime = fDoc->MimeType();

	if (mime && mime[0])
	{
		for (i = 0; i < fTypes->CountItems(); i++)
		{
			BMenuItem *item = fTypes->ItemAt(i);
			if (strcmp(item->Label(), mime) == 0)
			{
				item->SetMarked(true);
				break;
			}
		}

		if (i == fTypes->CountItems())
		{
			fTypes->AddSeparatorItem();
			fTypes->AddItem(new BMenuItem(mime, new BMessage(msg_FieldChanged)));
			fTypes->ItemAt(fTypes->CountItems() - 1)->SetMarked(true);
		}
	}
	else
	{
		BMenuItem *item;
		fTypes->AddSeparatorItem();
		fTypes->AddItem(item = new BMenuItem("<undefined>", new BMessage(msg_FieldChanged)));
		item->SetMarked(true);
	}

	if (fDoc->EntryRef())
	{
		BNode node;
		FailOSErr(node.SetTo(fDoc->EntryRef()));

		time_t t;
		node.GetModificationTime(&t);

		char time[256];
		strcpy(time, ctime(&t));
		time[strlen(time) - 1] = 0;

		SetText("time", time);
	}
	else
	{
		//SetEnabled("mime", false);
		SetText("time", "Not Saved");
	}

	mf = dynamic_cast<BMenuField*>(FindView("font"));
	FailNil(mf);

	fMenu = mf->Menu();
	FailNil(fMenu);

	font_family ff;
	font_style fs;

	for (int i = 0; i < count_font_families(); i++)
	{
		get_font_family(i, &ff);
		BMenu *fontItem = new BMenu(ff);
		FailNil(fontItem);
		fMenu->AddItem(new BMenuItem(fontItem, new BMessage(msg_FieldChanged)));
		fontItem->SetFont(be_plain_font);

		for (int j = 0; j < count_font_styles(ff); j++)
		{
			get_font_style(ff, j, &fs);

			BMessage *msg = new BMessage(msg_FieldChanged);
			msg->AddString("family", ff);
			msg->AddString("style", fs);
			fontItem->AddItem(new BMenuItem(fs, msg));
		}
	}

	fMenu->SetRadioMode(true);

	mf = dynamic_cast<BMenuField*>(FindView("encoding"));
	FailNil(mf);
	fEncoding = mf->Menu();
	FailNil(fEncoding);
	fEncoding->SetRadioMode(true);

	mf = dynamic_cast<BMenuField*>(FindView("source encoding"));
	FailNil(mf);
	fSourceEncoding = mf->Menu();
	FailNil(fSourceEncoding);
	fSourceEncoding->SetRadioMode(true);

	mf = dynamic_cast<BMenuField*>(FindView("linebreaks"));
	FailNil(mf);
	fLineBreaks = mf->Menu();
	FailNil(fLineBreaks);
	fLineBreaks->SetRadioMode(true);

	BTextControl *tc = dynamic_cast<BTextControl*>(FindView("tabs"));
	if (tc) tc->SetDivider(be_plain_font->StringWidth(tc->Label()) + 4);

	BMenu *lang;
	mf = dynamic_cast<BMenuField*>(FindView("language"));
	FailNil(mf);
	lang = mf->Menu();

	CLanguageInterface *intf;
	int cookie = 0;
	while ((intf = CLanguageInterface::NextIntf(cookie)) != NULL)
		lang->AddItem(new BMenuItem(intf->Name(), new BMessage(msg_FieldChanged)));

	lang->SetRadioMode(true);
	int curLang = fDoc->TextView()->Language();
	SetValue("language", curLang >= 0 ? curLang + 3 : 1);

	CancelClicked();
} /* CInfoDialog::CInfoDialog */
Example #29
0
void CCellView::HandleDrop(BMessage *inMessage)
{
	long l;
	void * p;
	BPoint dp = inMessage->DropPoint();
	
	fDragIsAcceptable = false;
	
	if (inMessage->FindPointer("container", &p) == B_NO_ERROR)
	{
		key_info ki;
		get_key_info(&ki);
		
		CContainer *srcContainer;
		range srcRange, dstRange;
		CCellView *srcView;
		int action;
		
		srcContainer = (CContainer *)p;
		FailOSErr(inMessage->FindData("range", 'rang', (const void**)&p, &l), errMessageMissing);
		srcRange = *(range *)p;
		FailOSErr(inMessage->FindPointer("cellview", &p), errMessageMissing);
		srcView = (CCellView *)p;
		
		if (srcView != this)
			action = dragCopy;
		else
			action = dragMove;
		
		inMessage->FindBool("dragacopy", &fDragACopy);
		
		if (srcView == this &&
		    (ki.modifiers & B_CONTROL_KEY ||
			fDragACopy))
		{
			BPopUpMenu popup("dragpopup", false);
			popup.SetFont(be_plain_font);
			popup.AddItem(new BMenuItem(GetMessage(msgLinkHere), NULL));
			popup.AddItem(new BMenuItem(GetMessage(msgMoveHere), NULL));
			popup.AddItem(new BMenuItem(GetMessage(msgCopyHere), NULL));
			popup.AddSeparatorItem();
			popup.AddItem(new BMenuItem(GetMessage(msgCancel), NULL));
			
			BMenuItem *item = popup.Go(dp, false, true);
			int result = item ? popup.IndexOf(item) : -1;
			switch (result)
			{
				case 0: action = dragLink; break;
				case 1: action = dragMove; break;
				case 2: action = dragCopy; break;
				default:
					ClearAnts();
					fCurCell = fSelection.TopLeft();
					return;
			}
		}
	
		dstRange = srcRange;
		dstRange.OffsetBy(fCurCell.h - dstRange.left,
			fCurCell.v - dstRange.top);
		
		((CCellWindow *)Window())->
			RegisterCommand(new CDragCommand(this, srcContainer,
				fContainer, &srcRange, &dstRange, action));
	}
	else if (inMessage->FindData("text/plain", B_MIME_DATA, (const void **)&p, &l) == B_NO_ERROR)
	{
		cell dc;
		
		ConvertFromScreen(&dp);
		GetCellHitBy(dp, dc);
		
		BMemoryIO buf(p, l);
		CContainer *srcContainer = new CContainer;
		range srcRange, dstRange;
		
		CTextConverter conv(buf, srcContainer);
		conv.ConvertFromText(srcRange);
		
		dstRange = srcRange;
		dstRange.OffsetBy(dc.h - srcRange.left, dc.v - srcRange.top);
		
		((CCellWindow *)Window())->
			RegisterCommand(new CDragCommand(this, srcContainer,
				fContainer, &srcRange, &dstRange, dragMove));

		srcContainer->Release();
	}
	else
	{
		beep();
		ClearAnts();
		fCurCell = fSelection.TopLeft();
	}
} /* CCellView::HandleDrop */
Example #30
0
void PApp::FindAndOpen(const char *file, const char* fromSource)
{
	char *bi = NULL;
	bool found = false;

	try
	{
		BEntry e;
		entry_ref doc;
		BDirectory d;

		if (!found && fromSource)
		{
			BPath path;
			entry_ref eref;
			if (get_ref_for_path(fromSource, &eref) == B_OK) {
				vector<BString> inclPathVect;
				if (!ProjectRoster->GetIncludePathsForFile(&eref, inclPathVect))
					ProjectRoster->GetAllIncludePaths(inclPathVect);

				for(uint32 i=0; !found && i<inclPathVect.size(); ++i)
				{
					if (path.SetTo(inclPathVect[i].String(), file) != B_OK)
						continue;
					if (e.SetTo(path.Path(), true) != B_OK)
						continue;
					if (e.Exists() && e.IsFile()) {
						FailOSErr(e.GetRef(&doc));
						found = true;
					}
				}
			}
		}

		if (!found && gPrefs->GetPrefInt(prf_I_BeIncludes))
		{
			bi = strdup(getenv("BEINCLUDES"));
			char *ip = bi;
			char *p = ip;

			while (p && !found)
			{
				char *pe = strchr(p, ';');
				if (pe) *pe = 0;

				FailOSErr(d.SetTo(p));
				if (d.Contains(file, B_FILE_NODE | B_SYMLINK_NODE))
				{
					FailOSErr(d.FindEntry(file, &e, true));
					if (!e.IsFile()) THROW((0));
					FailOSErr(e.GetRef(&doc));
					found = true;
				}

				p = (pe && pe[1]) ? pe + 1 : NULL;
			}
		}

		if (!found)
		{
			const char *p;
			int i = 0;

			while ((p = gPrefs->GetIxPrefString(prf_X_IncludePath, i++))!= NULL && !found)
			{
				if (e.SetTo(p) != B_OK || !e.Exists())
					continue;

				FailOSErr(d.SetTo(p));
				if (d.Contains(file, B_FILE_NODE | B_SYMLINK_NODE))
				{
					FailOSErr(d.FindEntry(file, &e, true));
					if (!e.IsFile()) THROW((0));
					FailOSErr(e.GetRef(&doc));
					found = true;
				}
			}
		}

		if (found)
			OpenWindow(doc);
		else
			beep();
	}
	catch (HErr& e)
	{
		beep();
	}

	if (bi)
		free(bi);
} // PApp::FindAndOpen